Devian Devian - 1 year ago 82
PHP Question

Form check information & insert on same page

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
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
function (which is in javascript) set
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 ?>;
echo 'submitted';
} else {
echo 'not true';

Answer Source

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

     $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 ?>')">
<button type="button" onclick="send(false)">

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 =,1); 
            /* Do POST here either by using XMLHttpRequest or jQuery AJAX/POST (Or any other way you like)*/
            /* XMLHttpRequest: */
            /* jQuery POST */
            console.log("No post")
            /* Don't post and do whatever you need to do otherwise */

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download