0

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();
    }
    
    
?>


 
Greg
  • 9
  • 2
  • you could put that *logic* in the PHP side, since you know the logic and the code is all yours ... or use javascript to send the request exactly as you require using AJAX – Bravo Mar 07 '22 at 23:26
  • 1
    Are A and B always the same opposites? If so, I don't see a need to store the not-clicked. You can derive its value from the clicked. – Tony Mar 07 '22 at 23:32
  • What's this got to do with the MySql tag? – Stu Mar 07 '22 at 23:54
  • @Tony If I understood correctly, no they are not always the same opposites. A and B are randomly selected from a SQL database table. Each option is unique so I need to be able to explicitly identify them. – Greg Mar 07 '22 at 23:59
  • One option: When the page loads, store all the possible options in the Session. Then you can compare the submitted option to the options stored in the Session, to know not only what was clicked, but also what wasn't clicked. – ADyson Mar 08 '22 at 00:29
  • The duplicate is specifically about select field options, but you can apply the same techniques to the value of an input field. – CBroe Mar 08 '22 at 08:48

1 Answers1

0

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>
Tony
  • 2,658
  • 2
  • 31
  • 46