简体   繁体   中英

How to submit the same form multiple times on same page and also process them on that page in php

I am trying to display a form and process data on same page multiple times.

This is my code:

<?php
function display($q) {      
    $que=mysqli_fetch_row($q);
    $_SESSION['qid']=$que[0];
    ob_start ();    
?>  

<form method="post" action="">
    <h3> <?php echo $que[0]." . ".$que[2]; ?> </h3>
    <input type="radio" name="ans" value="<?php echo $que[3]; ?>"> <?php echo $que[3]; ?> <br/>
    <input type="radio" name="ans" value="<?php echo $que[4]; ?>"> <?php echo $que[4]; ?> <br/>
    <input type="submit" name="next" value="next" >
</form>

<?php
    $_SESSION['qid']=$que[0];
    echo $_SESSION['qid'];
    if(isset($_POST['next'])) {
        if(array_key_exists('next',$_POST)){
            next_que();
        }
    }
}

function next_que() {
    //some code 
    // calling display function
}
?>

When I run this code, the display and next_que functions are working properly the first time, but on the next call only the display function is called displaying the form, and clicking the button doesn't turn the isset($_POST['next']) condition to true.

How can I get this to work?

Yes, prevent the form from submit via prevent default in your javascript. Then you can call your javascript functions / code on any button click and use the data in the same page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM