jemz Posted April 25, 2012 Share Posted April 25, 2012 Hi, I need some help please on this ajax,and i am still beginner on this.I created a simple page as my practice using ajax enabled.my problem is that it will not insert into my table.can you help me please what is the problem of my code. testajax.php <html><head> <script type="text/javascript"> function InsertDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById('display').innerHTML=xmlhttp.responseText; } } var name = encodeURI(document.getElementById('name').value); var email = encodeURI(document.getElementById('email').value); var address = encodeURI(document.getElementById('address').value); xmlhttp.open("GET","dbconn.php?name="+name+"&email="+email+"&address="+address,true); xmlhttp.send(); }</script></head> <body> <form > Name : <input type="text" id="name"> <br/> Email : <input type="text" id="email" > <br/> Address: <input type="text" id="address" > <br/><input type="submit" value="submit" onclick ="lnsertDoc();"> </form> <div id="display"></div> </body> </html> "dbconn.php" <?php $conn = mysql_connect('localhost','root',''); if(!$conn) { die("could not connect to localhost" .mysql_error());} mysql_select_db('dbajax',$conn); if (isset($_GET['name']) && isset($_GET['email']) && isset($_GET['address'])) { $name = $_GET['name']; $email = $_GET['email']; $address = $_GET['address']; $sql = "insert into ajaxTBl values('default', '$name', '$email', '$address')"; mysql_query($sql); echo "Successfully inserted"; } ?> Thank you in advance. Link to comment Share on other sites More sharing options...
dsonesuk Posted April 25, 2012 Share Posted April 25, 2012 try adding name attributes Name : <input type="text" id="name" name="name"> <br/> Email : <input type="text" id="email" name="email" > <br/> Address: <input type="text" id="address" name="address" > Link to comment Share on other sites More sharing options...
justsomeguy Posted April 25, 2012 Share Posted April 25, 2012 You need to return false to make sure the form does not submit, or else the page will refresh and the ajax request will get canceled: <input type="submit" value="submit" onclick ="lnsertDoc(); return false;"> Also, check the spelling of that function name. It's also better in that case to use encodeURIComponent instead of encodeURI. Link to comment Share on other sites More sharing options...
jemz Posted April 25, 2012 Author Share Posted April 25, 2012 You need to return false to make sure the form does not submit, or else the page will refresh and the ajax request will get canceled: <input type="submit" value="submit" onclick ="lnsertDoc(); return false;"> Also, check the spelling of that function name. It's also better in that case to use encodeURIComponent instead of encodeURI. Hi, Thank you for the quick reply...It's working thank you so much for helping me you are great!...can i ask?why is that in the url only testajax.php can be seen...i thouht it would be something like this testajax.php?name=name&email=email&address=address or encoded url because i use the encodeURIComponent.Once again Thank you.! Link to comment Share on other sites More sharing options...
thescientist Posted April 25, 2012 Share Posted April 25, 2012 You don't see it in the URL because you are making an AJAX request (which for loosely said, occurs under the hood, if you will). It is not the same thing as redirecting a user to a page with the query string attached. Link to comment Share on other sites More sharing options...
jemz Posted April 26, 2012 Author Share Posted April 26, 2012 You don't see it in the URL because you are making an AJAX request (which for loosely said, occurs under the hood, if you will). It is not the same thing as redirecting a user to a page with the query string attached. Hi thank you for enligthen my mind... :Happy: Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now