PHP using AJAX INSERT INTO MYSQL

Can't get it to insert into the database

Page 1 of 1

4 Replies - 24650 Views - Last Post: 20 October 2010 - 09:29 PM Rate Topic: -----

#1 radioluck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-November 08

PHP using AJAX INSERT INTO MYSQL

Posted 19 October 2010 - 06:58 PM

Hello there guys! I'm new with AJAX and I can't seem to find the solution to my problem in the internet. Some might find this too easy to do but I don't have any idea about how to use AJAX and I'm trying to self-study it, so here is my question, what is the syntax I need to insert the two values from my html page to my mysql database?

this one is the page where the input is made and I placed the AJAX
<html>
<head>
<script type="text/javascript">
function  insertUser()
{
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","insert.php",true);
xmlhttp.send("uname="document.getElementById("uname").value,pass="document.getElementById("pass").value");

}

</script>
</head> 
<body>

<form>
Firstname: <input type="text" name="uname" />
Lastname: <input type="text" name="pass" />
<input type="button" value="click me!" onclick="insertUser()"/>
</form>


</body>
</html> 



this is the php code
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_forum", $con);

$sql="INSERT INTO user (user_name,user_password)
VALUES
('$_POST[uname]','$_POST[pass]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 


the part with xmlhttp.send(); is the one I'm having trouble with, isnt that the part that I should put in the values I will send to the php file? or am I wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP using AJAX INSERT INTO MYSQL

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,036
  • Joined: 08-August 08

Re: PHP using AJAX INSERT INTO MYSQL

Posted 19 October 2010 - 07:35 PM

Here's how I do it:
var func = '';
var param = '';
var elem = '';

function myeval()
{
    var evalthis = func+'('+param+')';
	if(httpObject.readyState == 4)
	{
        eval(evalthis);
    }
}

function getHTTPObject()
{
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else 
	{
		alert("Your browser does not support AJAX.");
		return null;
	}
}

function changesomething(myphpurl, setchangedstate)
{
    httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", myphpurl);
		httpObject.send(null);
		httpObject.onreadystatechange = setchangedstate;
	} else {
		alert("oops!");
	}
}


function display_results(x)
{
	if(httpObject.readyState == 4)
	{
		alert("This is a parameter passed through the ajax process to this function: "+x);
        document.getElementById('some_element').innerHTML = httpObject.responseText;
	}
}

function add_info(x)
{
    var myurl = 'some_php_file.php?SomeKey='+x;
    func = 'display_results';
    param = x; // or some other value!
    changesomething(myurl,'myeval');
}


The 'add_info' function might be called on a submit or change event, and it calls changesomething after placing values in the global variables 'func' and 'param' which are used by eval to open the appropriate function 'func' to handle the response, along with any parameter 'param' I wish to pass to that function. The php file uses $_GET to receive the variable(s) being sent to it.

It may look complicated, but if you spend a little time with it you'll see that it works well and can be used to create many ajax events without adding too many additional functions.

This post has been edited by CTphpnwb: 19 October 2010 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

#3 radioluck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-November 08

Re: PHP using AJAX INSERT INTO MYSQL

Posted 19 October 2010 - 09:05 PM

what an interesting code snippet, sadly I am the very definition of "beginner" when it comes to AJAX, so I'm scratching my head right now, but I'll save your snippet so I can use it when I finally advance to the rank of "rookie" in terms of using AJAX.

Anyway sir I finally found the problem! The reason it wasn't inserting is because I set the primary key of the record to unique, it is int, and it is not set to auto-increment. My apologies for my stupidity. Anyway thanks so much! ^_^
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3494
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: PHP using AJAX INSERT INTO MYSQL

Posted 19 October 2010 - 10:41 PM

View Postradioluck, on 20 October 2010 - 01:58 AM, said:

the part with xmlhttp.send(); is the one I'm having trouble with, isnt that the part that I should put in the values I will send to the php file? or am I wrong?

you’re not wrong, you just got the syntax wrong. send() expects 1 parameter which would be equal to PHP’s $_SERVER['QUERY_STRING'] (and of course you still need to follow the Javascript syntax (your code should have thrown an error)). (ref)
xmlhttp.send("uname=" + document.getElementById("uname").value + "&pass=" + document.getElementById("pass").value);

Was This Post Helpful? 1
  • +
  • -

#5 radioluck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-November 08

Re: PHP using AJAX INSERT INTO MYSQL

Posted 20 October 2010 - 09:29 PM

I have a new problem guys!

How do I make a page load php when I open it? I'm doing a forum, and this is my first time. And can someone point me to a resource where I can learn how to make automatic links for topics in a forum? thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1