Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

onclick and php mysql query

click on a button then run the variable to query the db



3:31 am on Aug 31, 2009 (gmt 0)

5+ Year Member

hi, i have a new question ive been working on:
I have 4 options from buttons in php page.
i want to be able to click on a button then run the variable to query the db, and if another button is clicked, then the variable relating to that is queried etc...

Can it be done?

$menu1 = mysql_query("..."); //query the database tea table
$menu2 = mysql_query("...");

echo "<td>"."<input type='button' value='Teas' name='button1' onclick='".$menu1."'></td>";//
echo "<td><input type='button' value='Platters' name='button2' onclick='".$menu2."'></td>";//

if ($menu1) {...}
if ($menu2) {...}


3:50 am on Aug 31, 2009 (gmt 0)

5+ Year Member

you cant call a php function from onclick.

but you can make those buttons submit, and do this:

if(isset($_POST['button1'])) {
$q = mysql_query("INSERT INTO table (keys) VALUES ('$values')")or die(mysql_error());
if($q) {
echo "successful";
}else {
echo "error";


6:24 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

You can call a function onclick by using AJAX, but it gets kind of complicated. I would take tr8er8's suggestion and just make them buttons. Check out this simple example.
<form name="form" method="post">

<input type="submit" name="button1" value="Button 1" />
<input type="submit" name="button2" value="Button 2" />
<input type="submit" name="button3" value="Button 3" />
<input type="submit" name="button4" value="Button 4" />



echo("You clicked button one!");

}//if isset


echo("You clicked button two!");

}//if isset


echo("You clicked button three!");

}//if isset


echo("You clicked button four!");

}//if isset



11:48 pm on Aug 31, 2009 (gmt 0)

5+ Year Member

you guys are heros.. thanks a lot :)

it works, i never would have guessed, just very new to php. But it makes sence. I tried ifelse, but that didnt work, i didnt know about teh isset option :s

Thank you~


Featured Threads

Hot Threads This Week

Hot Threads This Month