Why is this Ajax presentation form not working?

What is wrong with the following code?

A warning returns success, but nothing is added to the table. The form:

<form id="Form_1" name="theform" method="post" action="">
    <label for="email" class="emailLab">Email * </label>
    <input type="email" name="email" id="email_id" autocorrect placeholder="Email" value="" />
    <label for="phone">Phone </label>
    <input type="tel" name="phone" id="phone_id" autocorrect placeholder="Phone" />
    <input type="image" src="images/btn_enter.png" height="26" width="147" id="submit" data-inline="true" data-role="none" />
</form>

In send mode, I call this function:

function serverDB (){
    // Define variables from input
    var vEmail = document.getElementById('email_id').value;
    var vPhone = document.getElementById('phone_id').value;

    var theData = 'email=' + vEmail + '&phone=' +vPhone;
    alert (theData);

    $.ajax({
        type: "POST",
        url: "process.php",
        data: theData,
        success: function(){
            alert ("Success");
        }
    });
}

And the PHP code:

<?
    $email  = $_POST['email'];
    $phone  = $_POST['phone'];
    mysql_connect("host", "user", "password") or die(mysql_error());
    mysql_select_db("sandbox_itouch") or die(mysql_error());
    mysql_query("INSERT INTO `data` (email, phone) VALUES ('$email','$phone')");
?>
+3
source share
4 answers

You are missing the "=" in this line after "& phone":

 var theData = 'email=' + vEmail + '&phone' +vPhone;

It could be a typo.

+1
source

Make sure that the $ _POST values ​​reach your script and that your MySQL connection is working properly and that your column and table names match the database.

You can create magazines like this

$fh = fopen('log.txt', 'w');
fwrite($fh, print_r($_POST, true));
fclose($fh);

If the log.txt file is not created, you will find out that the script is not running.

+1
source

, , script, , .

0

Try changing the data type in JSON or specify the data type explicitly when publishing:

var theData = '{ email:' + vEmail + ', phone:' + vPhone + '}' ;

$.ajax({
    type: "POST",
    url: "process.php",
    data: theData,
    success: function(){
        alert ("Success");
        $().innerHTML =
    }
});
0
source

All Articles