Jump to content

my simple ajax don't work


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...