homepage Welcome to WebmasterWorld Guest from 54.227.146.68
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
onclick and php mysql query
click on a button then run the variable to query the db
indiguy




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

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?

eg:
$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) {...}

 

tr8er8




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

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";
}
}

andrewsmd




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

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.
<html>
<body>
<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" />

</form>
</body>
<?php

if(isset($_POST['button1'])){

echo("You clicked button one!");

}//if isset

if(isset($_POST['button2'])){

echo("You clicked button two!");

}//if isset

if(isset($_POST['button3'])){

echo("You clicked button three!");

}//if isset

if(isset($_POST['button4'])){

echo("You clicked button four!");

}//if isset

?>
</html>

indiguy




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

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~

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved