'How to submit two pieces of related information with one submit click? [duplicate]
I have a simple "choose LEFT or RIGHT" app where the user selects their preference. When the user clicks their preference, I would like to record the option that was clicked and record that the other option wasn't clicked. Is this possible? I'm very new and am having a difficult time conceptualizing how to record the "non-click".
Here is the client-side stuff I have to record the click (preference) and refresh the options after the click.
<form action="submit.php" method="POST">
<input type="submit" value="Option A" onclick="refreshOptions()" name= "<?php OptionAClicked ?>" />
<input type="submit" value="Option B" onclick="refreshOptions()" name= "<?php OptionBClicked ?>" />
</form>
<script>
function refreshOptions() {
location.reload();
}
</script>
I don't know where to start. Could I simply add another name for the non-clicked option to each <input> like so?
SOLVED: I used additional "criss-cross" php logic and assigned ID keys to each option using a hidden pre-selected <select>. Also removed the refresh Javascript as this happens automatically with the submit.
server-side:
<?php
error_reporting(E_ERROR | E_PARSE);
include "pickOne.php";
$OptionAHit = $_POST['OptionA'];
$OptionBHit = $_POST['OptionB'];
$OptionAId = $_POST['OptionAId'];
$OptionBId = $_POST['OptionBId'];
if ($OptionAHit) {
$Hit = "UPDATE clicks SET Hits = Hits + 1 WHERE ? = OptionID";
$stmt = $con->prepare($Hit);
$stmt-> bind_param("i", $OptionAId);
$stmt->execute();
$Miss = "UPDATE clicks SET Misses = Misses + 1 WHERE ? = OptionID";
$stmt = $con->prepare($Miss);
$stmt-> bind_param("i", $OptionBId);
$stmt->execute();
$stmt->close();
$con->close();
} else {
$Hit = "UPDATE clicks SET Hits = Hits + 1 WHERE ? = OptionID";
$stmt = $con->prepare($Hit);
$stmt-> bind_param("i", $OptionBId);
$stmt->execute();
$Miss = "UPDATE clicks SET Misses = Misses + 1 WHERE ? = OptionID";
$stmt = $con->prepare($Miss);
$stmt-> bind_param("i", $OptionAId);
$stmt->execute();
$stmt->close();
$con->close();
}
?>
Solution 1:[1]
If the options have a unique identifier in the SQL database, use a separator to concatenate each one represented by the A/B pairing.
I would do something like the following, with both options listed in the value. You can pick apart the values in the form processing code, and each option is available for your record.
<form action="submit.php" method="POST">
<button type="submit" name="choice" value="0001-0004">Option A</button>
<button type="submit" name="choice" value="0004-0001">Option B</button>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tony |
