Jump to content

Script not working


cool_techie

Recommended Posts

my script is not working.I wanted to insert data from a form to db

using ajax and jquery...showing a loading icon as data is passed.

It shows that data inserted but does not insert.

 

 

form.html

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js">
</script>
<script type="text/javascript">
$(function()
{
$('#submit').click(function(){
$('#container').append('<img src="ajax-loader.gif" id="loading" />');

var name=$('#name').val();
var email=$('#email').val();
var comments=$('#comments').val();

$.ajax({
	url: 'process.php',
	type: 'POST',
	data: 'name=' + name + '&email=' + email + '&comments=' + comments,
	success: function(result){
		$('#response').remove();
		$('#container').append('<p id="response">' + result + '</p>');
		$('#loading').fadeOut(500);
	}
	});


});


});

</script>
</head>

<body>
<form action="process.php" method="post">
    <div id="container">

Name:<br />
<input type="text" name="name" id="name" /><br>
Email:<br />
<input type="text" name="email" id="email" /><br>
Comments:<br>
<textarea rows="5" cols="30" name="comments" id="comments">
</textarea><br>
<input type="submit" name="submit" id="submit" value="GO!" />
</div>
</form>

</body>
</html>

process.php:

<?php
$conns=new mysqli('localhost','root','','jquery_ex');
$query="INSERT into tab1 (name, email, comments) VALUES (?,?,?)";
$stmt=$conns->stmt_init();
if($stmt->prepare($query)){
$stmt->bind_param('sss','$_POST[name]','$_POST[email]','$_POST[comments]');
$stmt->execute();
}

if($stmt){
echo "Successfully inserted the data";
}else{
echo "Error";
}
?>

Link to comment
Share on other sites

Did you verify that the query works by itself?  Try adding some test values to the PHP so you have values to work with without requiring the ajax input.  Then create some code to echo the actual query that was submitted to the db. 

 

When I have issues that procedure usually helps me see what's going on. 

Link to comment
Share on other sites

One thing I see wrong is here

$stmt->bind_param('sss','$_POST[name]','$_POST[email]','$_POST[comments]');

 

You have your variables in single quotes...variables do not get evaluated in single quotes.  Either put them in double quotes or...don't use the quotes at all, seeing as how they aren't necessary.  But this may not be your only problem...you should at least be seeing those literal strings in being inserted...

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.