...

View Full Version : Update Mysql with function



Ndogg
03-02-2010, 06:18 AM
function accept($id)
{
mysql_query("UPDATE applications SET pending = 'Accepted' WHERE id = '$id'");
}

<input type="button" value="Accept" onclick="accept(<? echo $row['id']; ?>);">


The mysql connection is correct and eveything, it just says error in the bottom left with the little yellow warning sign.

Zangeel
03-02-2010, 06:47 AM
You're trying to use a php function as a javascript function. Wont work.

Ndogg
03-02-2010, 06:54 AM
lol, im always so confused about functions.

I thought this would work becuase of http://www.w3schools.com/php/php_functions.asp

How could I make it work?

Zangeel
03-02-2010, 07:06 AM
Many months ago, I explained, step by step, to you how to do almost this exact thing in This Thread (http://www.codingforums.com/showthread.php?t=173210).

Javascript, that needs to do server side functions requires using the ajax framework, to call and process other server pages in the background, so you'd have to call the php script with javascript.

CallumJohnson
03-02-2010, 09:12 AM
Javascript:


function confirmation_query(wmqueryID) {
var answer = confirm("Delete this submitted query?")
if (answer==true){
window.location = "?id=delete&queryID="+wmqueryID+"";
return true;
}
else{
return false;
}
}

Button:


<a onclick="confirmation_query(<?php echo $query['wmqueryID'];?>)">
<img src="../images/global/button_archive/small_delete.png" width="65" height="21" /></a>


Corresponding PHP:



//code executes if admin decides to delete query
if($id == "delete") {

$query_id = $_GET['queryID']; //Gets the querie's ID

//Rest of db code etc.

}//if ID=delete


May not be the best way but thats how i do it.

Shauny_B
03-02-2010, 02:03 PM
Hello,
Please escape the values before passing them into the database, this helps to minimize the rick of SQL injection.

To escape values use this PHP function:




$_GET["yourAttribute"] = mysql_real_escape_string($_GET["yourAttribute"]);

#Of if you're using the MySQLI extention which you need to have your server also supporting use this function below:

$_GET["yourAttribute"] = mysqli_real_escape_string($connection, $_GET["yourAttribute"]);



The above helps to escape any double quotes etc...

Happy coding,
Shaun

_Aerospace_Eng_
03-02-2010, 03:18 PM
Javascript:


function confirmation_query(wmqueryID) {
var answer = confirm("Delete this submitted query?")
if (answer==true){
window.location = "?id=delete&queryID="+wmqueryID+"";
return true;
}
else{
return false;
}
}

Button:


<a onclick="confirmation_query(<?php echo $query['wmqueryID'];?>)">
<img src="../images/global/button_archive/small_delete.png" width="65" height="21" /></a>


Corresponding PHP:



//code executes if admin decides to delete query
if($id == "delete") {

$query_id = $_GET['queryID']; //Gets the querie's ID

//Rest of db code etc.

}//if ID=delete


May not be the best way but thats how i do it.
Then why even use javascript at all if all your js does go to a link? Why not simply this

<a href="?id=delete&queryID=<?php echo $query['wmqueryID'];?>" onclick="return confirm('Delete this submitted query?')">
<img src="../images/global/button_archive/small_delete.png" width="65" height="21" /></a>
Then in your php

//code executes if admin decides to delete query
if($id == "delete") {

$query_id = intval($_GET['queryID']); //Gets the querie's ID

//Rest of db code etc.

}//if ID=delete

CallumJohnson
03-02-2010, 05:17 PM
@_Aerospace_Eng_:

Oh! Didn't know i could do it like that! Cheers :)

_Aerospace_Eng_
03-02-2010, 05:26 PM
@_Aerospace_Eng_:

Oh! Didn't know i could do it like that! Cheers :)

That way it will still work if JS is disabled but they just don't get the confirm message. As for the OP, a simple ajax request to a php file that takes in a GET or POST and then echos the result would be all you need.

Ndogg
03-03-2010, 01:15 AM
Many months ago, I explained, step by step, to you how to do almost this exact thing in This Thread (http://www.codingforums.com/showthread.php?t=173210).


Lol, i forgot about that.


But, for everything else, I already new how to do this by going to a different page or w/e, you know the ?=page thing, but what I want to try is make it all be done without changing or refreshing the page at all.

CallumJohnson
03-03-2010, 01:37 PM
But, for everything else, I already new how to do this by going to a different page or w/e, you know the ?=page thing, but what I want to try is make it all be done without changing or refreshing the page at all.

you might want to have a look at AJAX then



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum