Bahman Posted October 30, 2016 Share Posted October 30, 2016 Hi guys I want to send some data to my php page and in that page insert them to database,but dont work.I think Possibility jqury dont work. this is my code: Connection.php <?php $user="root"; $pass=""; $dsn="mysql:host=localhost;dbname=ajax"; try{ $coonnect=new PDO($dsn,$user,$pass); } catch(PDOException $error){ echo "Unable To connect Server".$error->getMessage(); print_r($e.errorInfo()); } ?> index.php <!doctype html> <html> <head> <script src="jquery-3.1.1.js"></script> <script> $(document).ready(function() { $(".btn").click(function(){ var name=$(".name").val(); var lname=$(".lname").val(); var birth=$(".birth").val(); var btn=true; $.post("ajax.php",{name:name,lname:lname,birth:birth,btn:btn},function(data){ $(".result"); alert("OK"); }); }); }); </script> <div class="result"> </div> </head> <body> <tr><td>name:</td><td><input type="text" class="name"></td></tr> <tr><td>last name:</td><td><input type="text" class="lname"></td></tr> <tr><td>birth:</td><td><input type="text" class="birth"></td></tr> <input type="submit" class="btn" value="ثبت"> </body> </html> ajax.php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php include("connection.php"); if(isset($_POST["btn"])){ if($_POST["name"]!="" && $_POST["lname"]!="" && $_POST["birth"]!=""){ $name=$_POST["name"]; $lname=$_POST["lname"]; $birth=$_POST["birth"]; $sql="INSERT INTO bus (name,lname,birth) VALUES (:name,:lname.:birth)"; global $sql; $result=$coonnect->prepare($sql); $result->bindParam(":name",$name); $result->bindParam(":lname",$lname); $result->bindParam(":birth",$birth); $result->execute(); }else{ echo "Please fill"; } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 31, 2016 Share Posted October 31, 2016 care to share any information on what symptom or error you got when you try your code. we are not sitting there with you and don't know what you saw in front of you. we also don't have the ability to run your code on your server. i do however have a recommendation, before you can use AJAX to accomplice a task, you must be able to write, test, and debug a html form and php form processing code. then, adding AJAX is as simple as adding an event listener that gets all the form data, makes the http request to the php form processing code, and prevents the default action of the browser submitting the form. Quote Link to comment Share on other sites More sharing options...
DeX Posted November 2, 2016 Share Posted November 2, 2016 To test what you're receiving on your ajax.php page, you can print it out with the following somewhere on the page: print_r($_POST); To test and see if your database insert is working at all, try replacing the POST variables with static entries. Once you narrow down where the error is, work on it from there. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.