cool_techie Posted February 11, 2011 Share Posted February 11, 2011 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 https://forums.phpfreaks.com/topic/227345-script-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 When posting code, please enclose it within the forum's . . . BBCode tags. Link to comment https://forums.phpfreaks.com/topic/227345-script-not-working/#findComment-1172777 Share on other sites More sharing options...
ManiacDan Posted February 11, 2011 Share Posted February 11, 2011 Fixed. Link to comment https://forums.phpfreaks.com/topic/227345-script-not-working/#findComment-1172794 Share on other sites More sharing options...
jcanker Posted February 12, 2011 Share Posted February 12, 2011 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 https://forums.phpfreaks.com/topic/227345-script-not-working/#findComment-1173316 Share on other sites More sharing options...
.josh Posted February 12, 2011 Share Posted February 12, 2011 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 https://forums.phpfreaks.com/topic/227345-script-not-working/#findComment-1173319 Share on other sites More sharing options...
ManiacDan Posted February 14, 2011 Share Posted February 14, 2011 To be clear, your quotes are misplaced. That line should be: $stmt->bind_param( 'sss', $_POST['name'], $_POST['email'], $_POST['comments'] ); -Dan Link to comment https://forums.phpfreaks.com/topic/227345-script-not-working/#findComment-1174039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.