jQuery, Ajax and PHP Request
-
I have created a simple form to insert data into a custom table. The problem is that while the Ajax request is recieving data, it is not passing that data onto the server to update the database any help will be appreciated.
First the code for the form.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="<?php bloginfo('stylesheet_directory'); ?>/tset.js"></script> </head> <body> <form method="get" name="testForm" id="testForm" action="https://www.paygate.co.za/paywebv2/process.trans"> <table> <tr> <td><label for="firstName">Name</label></td> <td><input type="text" id="firstName" name="firstName" /></td> </tr> <tr> <td><label for="lastName">Surname</label></td> <td><input type="text" id="lastName" name="lastName" /></td> </tr> <tr> <td><label for="age">Age</label> <td><input type="number" id="age" name="age" min="0" max="100" /></td> </tr> <tr> <td><input type="hidden" name="myHiddenField" id="myHiddenField" value="this is hidden" /></td> </tr> <tr> <td><button name="submit" id="submit">Update</button><td> </tr> </table> </form> </body> </html>
Then the code for the PHP
global $wpdb; $table = "tset"; $data = array( "First_Name" => $_POST["firstName"], "Last_Name" => $_POST["lastName"], "Age" => $_POST["age"], ); array( "%s", "%s", "%d", ); $wpdb -> insert($table, $data); $response = array("myHiddenField" => "this is now filled in"); echo json_encode($response);
Finally the js.
$j = jQuery.noConflict(); $j(document).ready(function(){ $j('#testForm').submit(submit_testForm); }); function submit_testForm() { $j.ajax({ url: "https://localhost/wordpress/wp-content/themes/responsive-mobile-child/tset.php", type: "POST", data: console.log($j('#testForm').serialize()), dataType: 'json', success: testForm_success }); return false; } function testForm_success() { $j("#myHiddenField").val(response.my_hidden_field); document.forms["testForm"].submit();
- The topic ‘jQuery, Ajax and PHP Request’ is closed to new replies.