0

So I've got a regular form

<form action="includes/send.php" method="post" onsubmit="return isValidForm()" />>

<h1>Opgeven workshops</h1>

<label for="name">Voornaam:</label>
<input type="text"  autocomplete="off" id="name" name="firstname">
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span>
</label>---more stuff more stuff more stuff--

Now I submit the form I want to show the information the user filled in in the form like this

$f_name = $_POST['firstname'];
$l_name = $_POST['lastname'];

U hebt zich ingeschreven bij: <br />
Eerste workshop : <?php echo $first; ?><br />
Tweede workshop : <?php echo $second; ?><br />
Klopt dit?

<button type="submit" onclick="send()">Ja</button>
<button type="submit" onclick="noSend()">nee</button>

and when the user clicks on send it sends the information from the previous form to the query to insert it into the database. I'm trying to do this without having to make another 'hidden form' which submits it again because it is unnecessary code when you can just let the script 'wait' and continue with the script / insert functionallity when the button is pressed.

I tried setting a variable $submit= false; and inside the send function (which is in javascript) set submit to true but that doesn't seem to work because it automatically sets the variable to true without pressing the button.

function send(){
<?php $submit = true ?>
var submit = <?php echo $submit ?>;
console.log(submit);
}
if($submit){
    echo 'submitted';
} else {
    echo 'not true';
}
Angel ofDemons
  • 1,267
  • 8
  • 13
Devian
  • 3
  • 2
  • JavaScript is client-side and PHP is server-side language. You cannot set PHP variable in JavaScript in the way you are trying to do. – ksno Jun 01 '16 at 07:43
  • You have no choice but to stores the values in hidden inputs or in session variables.. You can't let the script 'wait' like you said... – Naruto Jun 01 '16 at 07:44
  • I Know @ksno thats what I said. It doesn't work. Thats why I asked for suggestions – Devian Jun 01 '16 at 07:45
  • And @Naruto something like `prevent.default` wouldn't work? – Devian Jun 01 '16 at 07:45

1 Answers1

0

On your php side pass the values when calling your Javascript 'send()' function

<?php 
     $first = "First course";
     $second = "Second course"; 
?>


U hebt zich ingeschreven bij: <br />
Eerste workshop : <?php echo $first; ?><br />
Tweede workshop : <?php echo $second; ?><br />
Klopt dit?

<!-- pass the required values into the function, this is just a basic implementation you could also use a loop to fill in the values  -->

<button type="button" onclick="send(true, '<?php echo $first ?>', '<?php echo $second ?>')">
     Ja
</button> 
<button type="button" onclick="send(false)">
     nee
</button>

For the receiving function you could implement something like this

<script type="text/javascript">
    function send(submit){      
        //Get all arguments passed except the first variable, the submit boolean
        var listOfVariablesToPost = Array.prototype.slice.call(arguments,1); 
        if(submit){
            console.log("post");
            console.log(listOfVariablesToPost);
            /* Do POST here either by using XMLHttpRequest or jQuery AJAX/POST (Or any other way you like)*/
            /* XMLHttpRequest: http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest */
            /* jQuery POST https://api.jquery.com/jquery.post/ */
        }else{
            console.log("No post")
            /* Don't post and do whatever you need to do otherwise */
        }
    }
</script>

This is a very simple implementation, but I hope it helps.

Robbe Roels
  • 195
  • 4
  • 11