AJAX to send database null to PHP script

I am trying to use ajax to insert with a simple form into my database (using insert.php) for practice. var_dump($email)

Zero is removed below . Space script:

echo "Data for $name inserted successfully!";

      

The problem is that the variables are null as stated.

So we do it there, but the output is an empty variable field as shown below:

Data for inserted successfully!

Did I miss something?

index.php

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">

$(document).ready(function(){
//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#insert").click(function(){
//Get values of the input fields and store it into the variables.
var name=$("#name").val();
var email=$("#email").val();

//use the $.post() method to call insert.php file.. this is the ajax request
$.post('insert.php', {name: name, email: email},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500); //Fade in the data given by the insert.php file
});
return false;
});
});
</script>
</head>
<body>
<form>
<label>Name: </label> <input id="name" type="text" />
<label>E-Mail: </label> <input id="email" type="text" />
</form>
<a id="insert" title="Insert Data" href="#">Push into mysql</a>
 <!-- For displaying a message -->

<div id="message"></div>
</body>
</html>

      

insert.php

 <?php
//Configure and Connect to the Databse
 include "db_conx.php";
 if (!$db_conx) {
 die('Could not connect: ' . mysqli_error());
 }
 //Pull data from home.php front-end page
 $name=$_POST['name'];
 $email=$_POST['email'];
 echo "<pre>";
var_dump($email);
echo "</pre><br>";
 //Insert Data into mysql          INSERT INTO best_rate (name,email) 
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = mysqli_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>

      

UPDATE PHP # 2

<?php
//Configure and Connect to the Databse
 include "db_conx.php";
 if (!$db_conx) {
 die('Could not connect: ' . mysqli_error());
 }
 //Pull data from home.php front-end page
 $name=$_POST['myname'];
 $email=$_POST['myemail'];
 echo "<pre>";
var_dump($email);
echo "</pre><br>";
 //Insert Data into mysql          INSERT INTO best_rate (name,email) 
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = mysqli_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>

      

HTML # 2

<html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <!-- The ajax/jquery stuff -->
    <script type="text/javascript">

    $(document).ready(function(){
    //Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
    $("#insert").click(function(){
    //Get values of the input fields and store it into the variables.
    var name=$("#name").val();
    var email=$("#email").val();

    //use the $.post() method to call insert.php file.. this is the ajax request
    $.post('insert.php', {myname: name, myemail: email},
    function(data){
    $("#message").html(data);
    $("#message").hide();
    $("#message").fadeIn(1500); //Fade in the data given by the insert.php file
    });
    return false;
    });
    });
    </script>
    </head>
    <body>
    <form>
    <label>Name: </label> <input id="name" type="text" name="myname"/>
    <label>E-Mail: </label><input id="email" type="text" name="myemail"/>
    </form>
    <a id="insert" title="Insert Data" href="#">Push into mysql</a>
     <!-- For displaying a message -->

    <div id="message"></div>
    </body>
    </html>

      

Table structure

===============================================
id | name | email

      

db_conx.php

<?php
$db_conx = mysqli_connect("localhost", "user", "pass", "database");
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}
?>

      

+3


source to share


4 answers


I see that you have a problem with the post method, so we can use $ .get instead of $ .post and get the data in $ _GET ["name"]

I think this is the right decision now.



thank

+1


source


you have not provided a name that belongs to your feilds

<input id="name" type="text" />

      

use



<input id="name" type="text" name="myname"/>

      

and then used as in your php file

$name=$_POST['myname'];

      

+4


source


I have checked your code and it worked correctly, as I can see that there may be a problem connecting to the database or something mysql related. Your code is not working correctly, no need to specify name or any other parameter in HTML as you posted and set variable in jquery.

If you need more information, you need to provide the config file and table structure related to mysql so that I can check correctly.

thank

0


source


It seems to me that the values ​​from the inputs are not being passed to the php script to insert them.

I noticed in your code that you are passing an object containing these values:

$.post('insert.php', {myname: name, myemail: email},

      

I believe you have incorrectly set the name of the property (i.e. myname). In my opinion javascript breaks myname

as a variable, not a name. Correct code:

$.post('insert.php', {'myname': name, 'myemail': email},

      

Then this would set the POST variables correctly in your php code.

0


source







All Articles