-1

$(document).ready(function(){
$("#submit").click(function(){
var name = $("#name").val();
var email = $("#email").val();
var password = $("#password").val();
var contact = $("#contact").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'name1='+ name + '&email1='+ email + '&password1='+ password + '&contact1='+ contact;
if(name==''||email==''||password==''||contact=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});
<!DOCTYPE html>
<html>
<head>
<title>Submit Form Using AJAX and jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="css/refreshform.css" rel="stylesheet">
<script src="script.js"></script>
</head>
<body>
<div id="mainform">
<h2>Submit Form Using AJAX and jQuery</h2> <!-- Required div Starts Here -->
<div id="form">
<h3>Fill Your Information !</h3>
<div>
<label>Name :</label>
<input id="name" type="text">
<label>Email :</label>
<input id="email" type="text">
<label>Password :</label>
<input id="password" type="password">
<label>Contact No :</label>
<input id="contact" type="text">
<input id="submit" type="button" value="Submit">
</div>
</div>
</div>
</body>
</html>

This is what I have in ajaxsubmit.php

$host = "myhost"; 
$user = "myusername"; 
$password = "******"; 
$database = "thisismydb"; 

$connection = mysqli_connect($host, $user, $password, $database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Fetching Values from URL

$name2=$_POST['name1'];
$email2=$_POST['email1'];
$password2=$_POST['password1'];
$contact2=$_POST['contact1'];
//Insert query
mysqli_query($connection,"SELECT * FROM databasetable");
mysqli_query($connection,"INSERT INTO databasetable (name, email, password, contact) 
VALUES ($name2', '$email2', '$password2','$contact2')");


mysqli_close($connection); 
?>

However whenever i click submit it only gives me an alert that shows the code from the ajaxsubmit.php and i dont know what I'm doing wrong D: Help please!

note:i'm using bootstrap3

tash517
  • 171
  • 1
  • 4
  • 12
  • 1
    Check fourth parameter of `mysql_connect()` http://php.net/manual/en/function.mysql-connect.php – Saty Jul 05 '16 at 10:05
  • check 4th parameter in mysql_connect either use mysqli if you have practice on it. run the php file separately to debug. – Abhishek Gurjar Jul 05 '16 at 10:09
  • 1
    Your server don't execute `php` code – newage Jul 05 '16 at 10:10
  • @Abhishekgurjar i tried using mysqli it still doesnt work D: – tash517 Jul 05 '16 at 10:12
  • @C0dekid Thanks hahaha xD – tash517 Jul 05 '16 at 10:17
  • `$connection = mysqli_connect($host, $user, $password, $database);` if you are writing this then there is no need for use `$db = mysql_select_db($database, $connection);` and also you use mysqli_query($qurey,$conn); in right manner. – Abhishek Gurjar Jul 05 '16 at 10:17
  • http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php SQL injection, storing passwords in plaintext, please do some basic research. – Johan Jul 05 '16 at 12:24

1 Answers1

0

You need to write <?php tag at top of ajaxsubmit.php

Vardamir
  • 1
  • 1