简体   繁体   中英

Submit form with Ajax and insert data to MySQL not working

I am trying to submit a form using PHP and MySQL via Ajax, I am getting alert that form is submitted but no data inserted:

Following my code:

 <script> function myFunction() { var fname = document.getElementById("fname").value; var phone = document.getElementById("phone").value; var faddress = document.getElementById("faddress").value; var surveyername = document.getElementById("surveyername").value; var surveyurl = document.getElementById("surveyurl").value; // Returns successful data submission message when the entered information is stored in database. var dataString = 'fname1=' + fname + '&phone1=' + phone + '&faddress1=' + faddress + '&surveyername1=' + surveyername + '&surveyurl1=' + surveyurl; $.ajax({ type: "POST", url: "index.php", data: dataString, cache: false, success: function(html) { alert("Form Submitted"); } }); return false; } </script> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form"> <label>Name :</label> <input id="fname" type="text"><br> <label>Phone :</label> <input id="phone" type="text"> <label>Address :</label><br> <input id="faddress" type="text"> <label>Surveyer Name :</label><br> <input id="surveyername" type="text"> <input id="surveyurl" type="hidden" value="survey-url"><br> <input id="submit" onclick="myFunction()" type="button" value="Submit"> <button type="submit" class="btn btn-lg custom-back-color" onclick="myFunction()">Submit form</button> </div> <!-- PHP code --> <?php // Fetching Values From URL $fname2 = $_POST['fname1']; $phone2 = $_POST['phone1']; $faddress2 = $_POST['faddress1']; $surveyername2 = $_POST['surveyername1']; $surveyurl2 = $_POST['surveyurl1']; $connection = mysqli_connect("localhost", "dbuser", "dbpass"); // Establishing Connection with Server.. if($connection === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $sql = "INSERT INTO form_element (fname, phone, faddress, surveyername, surveyurl) VALUES ('$fname2', '$phone2', '$faddress2','$surveyername2','$surveyurl2')"; if(mysqli_query($connection, $sql)){ echo "Records inserted successfully."; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } mysqli_close($connection); // Connection Closed ?> 

EDIT:

CREATE TABLE form_element(
 fname varchar(255) NOT NULL,
 phone varchar(255) NOT NULL,
 faddress varchar(255) NOT NULL,
 surveyername varchar(255) NOT NULL,
 surveyurl varchar(255) NOT NULL
);

First,it's bad practice to write parameter directly into your sql,it might led to SQL Injection ,you had better use preparestatement to set the parameter.

Just for your problem,the reason is that,you have not pass the parameter directly to the sql

change

$sql = "INSERT INTO form_element (fname, phone, faddress, surveyername, surveyurl)
 VALUES ('$fname2', '$phone2', '$faddress2','$surveyername2','$surveyurl2')";

to

$sql = "INSERT INTO form_element (fname, phone, faddress, surveyername, surveyurl) 
     VALUES ('".$fname2."', '".$phone2."', '".$faddress2."','".$surveyername2."','".$surveyurl2."')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM