get button click event handler(dynamically created buttons)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

get button click event handler(dynamically created buttons)

Post by lmh85 »

hope anyone can help me in it.

here it goes. I had created a set of buttons by setting array in their names such as

Code: Select all

using a for loop..
<input type=\"submit\"value=\"Add To Cart\" class=\"btnListAdd\"name=\"btnAdd[$rowPdtCount[0]]\" id=\"btnAdd[$rowPdtCount[0]]\" onClick=\"addToCart($rowPdtCount[0]);\">
after which, i need to get the button clicked event but i hit an error.

Code: Select all

foreach ($_POST['btnAdd'] as $key => $value){
if(isset($_POST['btnAdd[$key]'])){
..blabla..
}
and i gotten nothing. is it how its suppose to be done?sorry, a real noob here.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

what does the html code look like after php has outputted it? (use the view source option and copy/paste back here...)
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

nickvd wrote:what does the html code look like after php has outputted it? (use the view source option and copy/paste back here...)

Code: Select all

<table width="100%" cellspacing="8"><tr><td><hr><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002001<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002001]" id="btnAdd[DF002001]"></td></tr></table><hr></td></tr><tr><td><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002003<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002003]" id="btnAdd[DF002003]"></td></tr></table><hr></td></tr>
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

i think you need to use onclick event of javascript on button
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: get button click event handler(dynamically created butto

Post by volka »

lmh85 wrote:but i hit an error.
Is it a secret error message or can you share it with us?

Try

Code: Select all

echo '<pre>'; print_r($_POST); echo "</pre>\n";
right before your foreach loop. Does the array contain the elements/values you're expecting?
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Re: get button click event handler(dynamically created butto

Post by lmh85 »

volka wrote:
lmh85 wrote:but i hit an error.
Is it a secret error message or can you share it with us?

Try

Code: Select all

echo '<pre>'; print_r($_POST); echo "</pre>\n";
right before your foreach loop. Does the array contain the elements/values you're expecting?
these buttons is created when i pop information out of the database. there's error on my foreach. Because there's nothing found. I need to put the data into $_SESSION.. is it possible to be done in javascript? or is there anyway i can do it in php by detecting the button click? :(
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Re: get button click event handler(dynamically created butto

Post by lmh85 »

volka wrote:
lmh85 wrote:but i hit an error.
Is it a secret error message or can you share it with us?

Try

Code: Select all

echo '<pre>'; print_r($_POST); echo "</pre>\n";
right before your foreach loop. Does the array contain the elements/values you're expecting?
hi there, i had tried echoing out. There's nothing in the array. it shows..

Code: Select all

Array
(
)


Warning: Invalid argument supplied for foreach() in C:\www\prdts.php on line 3
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

lmh85 wrote:

Code: Select all

<table width="100%" cellspacing="8"><tr><td><hr><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002001<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002001]" id="btnAdd[DF002001]"></td></tr></table><hr></td></tr><tr><td><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002003<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002003]" id="btnAdd[DF002003]"></td></tr></table><hr></td></tr>
Is there a form element? Or how do you set the POST method?
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

volka wrote:
lmh85 wrote:

Code: Select all

<table width="100%" cellspacing="8"><tr><td><hr><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002001<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002001]" id="btnAdd[DF002001]"></td></tr></table><hr></td></tr><tr><td><table border="0"><tr><td class="bodyTextNorm" width="75%"><a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref">DF002003<a href="javascript:void(0)" onclick="popupDetails(1)" class="ahref"> <b>product details</b></a></td><td class="bodyTextNorm" width="12%"><div class="bodyText">Qty : <input type="text" name="txtQty" value="0" size="1" maxlength="2" class="bodyTextNorm"></div></td><td width="13%" align="right"><input type="submit"value="Add To Cart" class="btnListAdd"name="btnAdd[DF002003]" id="btnAdd[DF002003]"></td></tr></table><hr></td></tr>
Is there a form element? Or how do you set the POST method?
oops.. there's no form element in this page. sorry, may i know what do u mean by setting the POST method?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You're using $_POST in your php script.
http://www.php.net/manual/en/reserved.v ... quote]HTTP POST variables: $_POST
...
An associative array of variables passed to the current script via the HTTP POST method.[/quote]The default is GET which does not pop up in $_POST. You must instruct the client that it shall pass the parameters via POST.

Code: Select all

<form method="post" action="xyz.php">
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

volka wrote:You're using $_POST in your php script.
http://www.php.net/manual/en/reserved.v ... quote]HTTP POST variables: $_POST
...
An associative array of variables passed to the current script via the HTTP POST method.
The default is GET which does not pop up in $_POST. You must instruct the client that it shall pass the parameters via POST.

Code: Select all

<form method="post" action="xyz.php">
[/quote]

i had added form tag but it still can't work.. :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

current code?
(sorry, I'm still no clairvoyant)
lmh85
Forum Commoner
Posts: 49
Joined: Sat Oct 28, 2006 10:49 am

Post by lmh85 »

i had added the form tag and it works now! :D i added the form tag wrongly.. thanks thanks!
Post Reply