Page 1 of 1

get button click event handler(dynamically created buttons)

Posted: Sun Oct 29, 2006 7:57 pm
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.

Posted: Sun Oct 29, 2006 8:29 pm
by nickvd
what does the html code look like after php has outputted it? (use the view source option and copy/paste back here...)

Posted: Sun Oct 29, 2006 8:40 pm
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>

Posted: Mon Oct 30, 2006 2:29 am
by itsmani1
i think you need to use onclick event of javascript on button

Re: get button click event handler(dynamically created butto

Posted: Mon Oct 30, 2006 3:17 am
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?

Re: get button click event handler(dynamically created butto

Posted: Mon Oct 30, 2006 7:39 am
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? :(

Re: get button click event handler(dynamically created butto

Posted: Mon Oct 30, 2006 7:42 am
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

Posted: Mon Oct 30, 2006 7:47 am
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?

Posted: Mon Oct 30, 2006 7:55 am
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?

Posted: Mon Oct 30, 2006 8:07 am
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">

Posted: Mon Oct 30, 2006 8:09 am
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.. :(

Posted: Mon Oct 30, 2006 8:13 am
by volka
current code?
(sorry, I'm still no clairvoyant)

Posted: Mon Oct 30, 2006 8:16 am
by lmh85
i had added the form tag and it works now! :D i added the form tag wrongly.. thanks thanks!