When i click the save button after filling the form it should be saved in the database as well as should be redirected to the edit page where i can edit the fields.
            Asked
            
        
        
            Active
            
        
            Viewed 1,860 times
        
    -3
            
            
        - 
                    Send Ajax request on successful return redirect to another page – NullPoiиteя Sep 12 '16 at 09:18
- 
                    but the adding of data is done through ajax only – Ninad Sep 12 '16 at 09:30
- 
                    @Ninad im typing an example but i will use PDO is that ok ? – Laith Sep 12 '16 at 09:31
- 
                    Possible duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – Boris Schegolev Sep 12 '16 at 10:49
1 Answers
0
            
            
        The trick is to POST the form to the current page, save your data into the database using your favorite method and then redirecting to the next page.
Take a look at the following example:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Insert into database
    $stmt = "INSERT INTO table (something) VALUES ('".$_POST['data']."')";
    // Redirect
    header("Location: nextpage.php");
    exit;
}
?>
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" role="form">
    <div>
        <label for="data">Your data to insert into the database</label>
        <input type="text" name="data" id="data" placeholder="Data to insert">
    </div>
    <button type="submit">Save</button>
</form>
 
    
    
        Peter
        
- 8,776
- 6
- 62
- 95
