Page 1 of 1

instantly updating php database

Posted: Sat Oct 06, 2007 6:21 pm
by suthie
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to make data be sent to a mySQL database instatnly on the click of a button:

[syntax="html"]<INPUT TYPE=IMAGE src="images/post_private.png" OnClick="Java_Up(1);">
//the number will vary, 1 is just an example


and then the page will update without reloading... so far I am using this javascript:[/syntax]

Code: Select all

<script language="JavaScript">
<!-- Begin
function Java_Up(P_NUM) {
<?PHP 

//this is where i do not know what to do. how can i get the post number from the javasrcipt function into this php?? (P_NUM)

$sql = "SELECT * from table WHERE postid='$postnum,' ORDER BY postid DESC";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)){
$prev = stripslashes($row['shared']);
if($prev == "false"){     
$result = mysql_query("UPDATE table_digposts SET shared='true' WHERE postid='$postnum' LIMIT 1") or print ("Can't update entry.<br />" . mysql_error());
} else {   
$result = mysql_query("UPDATE table_digposts SET shared='false' WHERE postid='$postnum' LIMIT 1") or print ("Can't update entry.<br />" . mysql_error());
}
?>
}
// End -->
</script>
I got stuck trying to transfer data from the javascript function to the php part of it.... how can i do this?? or is there a better way to do an instand update thing?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Oct 06, 2007 6:55 pm
by Christopher
You can't access the database from Javascript in the browser. You need to pass the data in the form to a PHP script on the server. In PHP you can get the values out of the superglobal array variables $_POST or $_GET. Then insert them into the SQL string and do the query.

Posted: Sat Oct 06, 2007 7:30 pm
by califdon
As arborint said, you can't directly access a database from a client side script. But you can use Ajax (which isn't a separate language, it's just using Javascript and PHP together) to have Javascript send a request to a server script (PHP or any other server side script) to return the data without reloading the web page containing the Javascript. If you don't know anything about Ajax, check out the vast amount of information on the net.

Posted: Sat Oct 06, 2007 8:15 pm
by suthie
AJAX is what i was attempting to do... is any part of this done right?

is ASP.net AJAX different?

Posted: Sun Oct 07, 2007 2:45 pm
by califdon
suthie wrote:AJAX is what i was attempting to do... is any part of this done right?

is ASP.net AJAX different?
Apparently you tried to attach some code, but it wasn't posted, perhaps due to some forum rules.

There should be no difference with ASP.NET, since Ajax is nothing more and nothing less than using Javascript's built-in object, XMLHttpRequest. The script on the server side can be absolutely anything you want, ASP, PHP, Perl, Python, CGI, as long as it returns a text response to the Javascript object, which is waiting for it.

There are so many examples of Ajax on the net that I recommend that you read some of them and check your own code. I don't mean to be unhelpful, but not many of us can devote the time necessary to examine a lengthy script for someone else, when we usually have our own debugging problems with what we're working on.

Posted: Sun Oct 07, 2007 2:58 pm
by califdon
I went back and re-read your original posting, and I see your major area of confusion, so let me try to help you understand. PHP is a server script that is executed before anything is sent to the browser. Javascript is a script that runs in the browser, after everything has been received from the server. This is why you cannot mix them, as you tried to do in your Java_Up() Javascript. By the time your Javascript function is called, it is in the browser, too late for PHP to execute!

To use Ajax, you have to have a Javascript function that instantiates an object of the XMLHttpRequest class and defines what Javascript (browser) function will be called after the response has been received from the server. At the end of the function that instantiates the XMLHttpRequest object, you then call the server script, which is a completely separate PHP (or other) script on the server, which does whatever it needs to do to get the data, then echo's it back to the Javascript object that is waiting for it.

I hope that will help you get started.