Jump to content

jquery dont send data to other page


Bahman

Recommended Posts

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.