By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

forms with dynamic inputs

P: n/a
Hi Gurus

I have a php page with a form in it, that is along the lines of:

<form...>
item with id 234
<input type=submit ... NAME=add234 ID=add234>
<input type=hidden NAME=add234 ID=234>
item with id 235
<input type=submit ... NAME=add235 ID=add235>
<input type=hidden NAME=add235 ID=235>
[....etc....]
</form>

In the PHP code at the top of the document, I want to:

a. intercept any submit from the form
b. get the related ID number

At the moment, I have only one add button, so I use code like this:

if($HTTP_POST_VARS[add]){
...
.....
}

So my questions are:

A. how can I change

if($HTTP_POST_VARS[add]){

to make it intercept any submit button that starts with add

B. how can I get the corresponding ID number
TIA

- Nicolaas
Jul 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
WindAndWaves wrote:
Hi Gurus

I have a php page with a form in it, that is along the lines of:

<form...>
item with id 234
<input type=submit ... NAME=add234 ID=add234>
<input type=hidden NAME=add234 ID=234>
item with id 235
<input type=submit ... NAME=add235 ID=add235>
<input type=hidden NAME=add235 ID=235>
[....etc....]
</form>

In the PHP code at the top of the document, I want to:

a. intercept any submit from the form
b. get the related ID number

At the moment, I have only one add button, so I use code like this:

if($HTTP_POST_VARS[add]){
...
....
}

So my questions are:

A. how can I change

if($HTTP_POST_VARS[add]){

to make it intercept any submit button that starts with add

B. how can I get the corresponding ID number
TIA

- Nicolaas


You can't get at the element id from within PHP, as that lives in the
client. Instead, give the submit buttons the same name (e.g.,
"submitted_id") but a unique value. Then when any submit button is
clicked, the common name will have the unique value:

item with id 234
<input type='submit' ... name='submitted_id' value='add234'>
item with id 235
<input type='submit' ... name='submitted_id' value='add235'>
item with id 236
<input type='submit' ... name='submitted_id' value='add236'>
Then in your code:

$item_id = $HTTP_POST_VARS['submitted_id'];

$item_id will be one of 'add234', 'add235', or 'add236'.

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #2

P: n/a

"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:
Hi Gurus

I have a php page with a form in it, that is along the lines of:

<form...>
item with id 234
<input type=submit ... NAME=add234 ID=add234>
<input type=hidden NAME=add234 ID=234>
item with id 235
<input type=submit ... NAME=add235 ID=add235>
<input type=hidden NAME=add235 ID=235>
[....etc....]
</form>

In the PHP code at the top of the document, I want to:

a. intercept any submit from the form
b. get the related ID number

At the moment, I have only one add button, so I use code like this:

if($HTTP_POST_VARS[add]){
...
....
}

So my questions are:

A. how can I change

if($HTTP_POST_VARS[add]){

to make it intercept any submit button that starts with add

B. how can I get the corresponding ID number
TIA

- Nicolaas


You can't get at the element id from within PHP, as that lives in the
client. Instead, give the submit buttons the same name (e.g.,
"submitted_id") but a unique value. Then when any submit button is
clicked, the common name will have the unique value:

item with id 234
<input type='submit' ... name='submitted_id' value='add234'>
item with id 235
<input type='submit' ... name='submitted_id' value='add235'>
item with id 236
<input type='submit' ... name='submitted_id' value='add236'>
Then in your code:

$item_id = $HTTP_POST_VARS['submitted_id'];

$item_id will be one of 'add234', 'add235', or 'add236'.

Thank you for your reply. That is simpler than I thought. The only problem
I have now is that I don't want the text "add..." to appear on page. All I
want the user to see is "add to basket"....

Do you have any hints on this?
Jul 17 '05 #3

P: n/a
WindAndWaves wrote:
"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:
Hi Gurus

I have a php page with a form in it, that is along the lines of:

<form...>
item with id 234
<input type=submit ... NAME=add234 ID=add234>
<input type=hidden NAME=add234 ID=234>
item with id 235
<input type=submit ... NAME=add235 ID=add235>
<input type=hidden NAME=add235 ID=235>
[....etc....]
</form>

In the PHP code at the top of the document, I want to:

a. intercept any submit from the form
b. get the related ID number

At the moment, I have only one add button, so I use code like this:

if($HTTP_POST_VARS[add]){
...
....
}

So my questions are:

A. how can I change

if($HTTP_POST_VARS[add]){

to make it intercept any submit button that starts with add

B. how can I get the corresponding ID number
TIA

- Nicolaas


You can't get at the element id from within PHP, as that lives in the
client. Instead, give the submit buttons the same name (e.g.,
"submitted_id") but a unique value. Then when any submit button is
clicked, the common name will have the unique value:

item with id 234
<input type='submit' ... name='submitted_id' value='add234'>
item with id 235
<input type='submit' ... name='submitted_id' value='add235'>
item with id 236
<input type='submit' ... name='submitted_id' value='add236'>
Then in your code:

$item_id = $HTTP_POST_VARS['submitted_id'];

$item_id will be one of 'add234', 'add235', or 'add236'.


Thank you for your reply. That is simpler than I thought. The only problem
I have now is that I don't want the text "add..." to appear on page. All I
want the user to see is "add to basket"....

Do you have any hints on this?


Whoops! I forgot that the value appears on the button. Instead of
using <input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>

You could even drop the "add" from the value so that you just get the
number back.

NM
--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #4

P: n/a

"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:
"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:

Hi Gurus

I have a php page with a form in it, that is along the lines of:

<form...>
item with id 234
<input type=submit ... NAME=add234 ID=add234>
<input type=hidden NAME=add234 ID=234>
item with id 235
<input type=submit ... NAME=add235 ID=add235>
<input type=hidden NAME=add235 ID=235>
[....etc....]
</form>

In the PHP code at the top of the document, I want to:

a. intercept any submit from the form
b. get the related ID number

At the moment, I have only one add button, so I use code like this:

if($HTTP_POST_VARS[add]){
...
....
}

So my questions are:

A. how can I change

if($HTTP_POST_VARS[add]){

to make it intercept any submit button that starts with add

B. how can I get the corresponding ID number
TIA

- Nicolaas

You can't get at the element id from within PHP, as that lives in the
client. Instead, give the submit buttons the same name (e.g.,
"submitted_id") but a unique value. Then when any submit button is
clicked, the common name will have the unique value:

item with id 234
<input type='submit' ... name='submitted_id' value='add234'>
item with id 235
<input type='submit' ... name='submitted_id' value='add235'>
item with id 236
<input type='submit' ... name='submitted_id' value='add236'>
Then in your code:

$item_id = $HTTP_POST_VARS['submitted_id'];

$item_id will be one of 'add234', 'add235', or 'add236'.


Thank you for your reply. That is simpler than I thought. The only problem I have now is that I don't want the text "add..." to appear on page. All I want the user to see is "add to basket"....

Do you have any hints on this?


Whoops! I forgot that the value appears on the button. Instead of
using <input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>

You could even drop the "add" from the value so that you just get the
number back.


you are a genius. I should have thought about it myself!

Thank you
Jul 17 '05 #5

P: n/a
News Me wrote:

Whoops! I forgot that the value appears on the button. Instead of
using <input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>


(I'm sleepy...) I guess that should have been:

<button type="submit" name="submitted_id" value="234">Add To Basket
</button>

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #6

P: n/a

"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
News Me wrote:

Whoops! I forgot that the value appears on the button. Instead of
using <input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>


(I'm sleepy...) I guess that should have been:

<button type="submit" name="submitted_id" value="234">Add To Basket
</button>

NM

Hi NM

I have the following:

<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotrip">add
to trip</BUTTON>

but it tells me that the value of the add button is "add to trip" and not
14, as I would have liked ....

what am I doing wrong?

- Nicolaas
Jul 17 '05 #7

P: n/a
WindAndWaves wrote:
"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
News Me wrote:

Whoops! I forgot that the value appears on the button. Instead of
using <input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>

(I'm sleepy...) I guess that should have been:

<button type="submit" name="submitted_id" value="234">Add To Basket
</button>

NM


Hi NM

I have the following:

<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotrip">add
to trip</BUTTON>

but it tells me that the value of the add button is "add to trip" and not
14, as I would have liked ....

what am I doing wrong?

- Nicolaas


Replace this:

ID="add14"

with this:

VALUE="add14"

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #8

P: n/a
WindAndWaves wrote:

Ignore that last message, my eyes are tired...

Hmmm... I'll have to try this myself.

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #9

P: n/a
On Sat, 15 Jan 2005 00:38:49 -0800, News Me wrote:
Whoops! I forgot that the value appears on the button. Instead of using
<input type="submit" ...>, use a <button> like this:

<button type="submit" name="submitted_id" value="add234">BUTTON
TEXT</button>

You could even drop the "add" from the value so that you just get the
number back.

NM


Stay clear of the <button> element!

IE has a bad implementation in which the name / value pairs of ALL
<button> elements within the form are submitted - not just the one that
was clicked.

Use submit buttons with name of add_{$id}. Simply grep (ie run a regex
on) all the form values submitted for "add_\d+" (assuming that all IDs are
numeric).

HTH,
La'ie Techie

Jul 17 '05 #10

P: n/a

"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:

Ignore that last message, my eyes are tired...

Hmmm... I'll have to try this myself.

NM

I actually discovered that if I do not make it input type="submit" that I
run into other difficulties: the data is not really reposted and the old
data is kept instead. IN that way the user can only add the first product.

Well, anyway, let me knows

THANKS a lot for your help.
Jul 17 '05 #11

P: n/a

"La?ie Techie" <laie@win_remove_get_nospam_solutions.com> wrote in message
news:1105781619.e9541b67e05391c93e4ff68fcf4dc4de@t eranews...
On Sat, 15 Jan 2005 00:38:49 -0800, News Me wrote:
[....]
Use submit buttons with name of add_{$id}. Simply grep (ie run a regex
on) all the form values submitted for "add_\d+" (assuming that all IDs are
numeric).

HTH,
La'ie Techie


Could you translate that to novice PHP, sorry, but this is my very first
site that I have taken over from someone, and it is all a bit new to me.

I have changed it back to a submit button, I can add the
name="'.$rowIdnumber.'", but the rest I don't understand at all - lol

- Nicolaas
Jul 17 '05 #12

P: n/a
WindAndWaves wrote:
Hi NM

I have the following:

<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotrip">add
to trip</BUTTON>

but it tells me that the value of the add button is "add to trip" and not
14, as I would have liked ....

what am I doing wrong?


I just put the following onto a test page:

<form action="" method="get">
<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14"
CLASS="addtotrip">add to trip</BUTTON>
<BUTTON TYPE="submit" NAME="add" VALUE="15" ID="add15"
CLASS="addtotrip">add to trip</BUTTON>
</form>

And when I press the first button the URL changes to this:

http://localhost/test/?add=14

and when I press the second button I get this:

http://localhost/test/?add=15

So the correct value is being returned. How are you extracting the
value in your PHP code?

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #13

P: n/a

"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...
WindAndWaves wrote:
Hi NM

I have the following:

<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14" CLASS="addtotrip">add to trip</BUTTON>

but it tells me that the value of the add button is "add to trip" and not 14, as I would have liked ....

what am I doing wrong?


I just put the following onto a test page:

<form action="" method="get">
<BUTTON TYPE="submit" NAME="add" VALUE="14" ID="add14"
CLASS="addtotrip">add to trip</BUTTON>
<BUTTON TYPE="submit" NAME="add" VALUE="15" ID="add15"
CLASS="addtotrip">add to trip</BUTTON>
</form>

And when I press the first button the URL changes to this:

http://localhost/test/?add=14

and when I press the second button I get this:

http://localhost/test/?add=15

So the correct value is being returned. How are you extracting the
value in your PHP code?

This is all really:

<?php
session_start();

// Initialize variables
$errormessage = "";
$sqlStart = "SELECT a.id, a.FN, a.name, a.description, a.photolink,
a.regionId FROM friars a, regions b";
$sqlWhere = " WHERE (a.regionId = b.id) AND ";
$sqlEnd = " GROUP BY a.regionId, a.name ";
$humanSQL = "<UL>";
$maxrows = 50;

// If no sortorder is set, initialize it to 1
if(!$sortorder){
$_SESSION["sortorder"] = 1;
}
//If an Accommodation is to be added
if($HTTP_POST_VARS[add]){

// Add accomodation to the session with an incremented sortorder
$_SESSION["sortorder"] = $sortorder + 1;

$id = 0;
$id = $HTTP_POST_VARS['add'];
echo '***************** ID = '.$id;

// Select DB data for accommodation to ADD and Append to Session
include_once("connectDB.php");
$sqladd = "SELECT * FROM friars WHERE id = $id";
$resultAdd = mysql_query($sqladd);
$myrowAdd = mysql_fetch_array($resultAdd);

.....lots of other code

?>
<html .....

<button ..[as discussed]..>

..... /html>
Jul 17 '05 #14

P: n/a
WindAndWaves wrote:

[snip]

<?php
session_start();

// Initialize variables
$errormessage = "";
$sqlStart = "SELECT a.id, a.FN, a.name, a.description, a.photolink,
a.regionId FROM friars a, regions b";
$sqlWhere = " WHERE (a.regionId = b.id) AND ";
$sqlEnd = " GROUP BY a.regionId, a.name ";
$humanSQL = "<UL>";
$maxrows = 50;

// If no sortorder is set, initialize it to 1
if(!$sortorder){
$_SESSION["sortorder"] = 1;
}
//If an Accommodation is to be added
if($HTTP_POST_VARS[add]){

// Add accomodation to the session with an incremented sortorder
$_SESSION["sortorder"] = $sortorder + 1;

$id = 0;
$id = $HTTP_POST_VARS['add'];
echo '***************** ID = '.$id;


[snip]

Looks like that should work to me. I just read La'ie Techie's post.
Are you using IE? I'm using FireFox.

NM

--
convert uppercase WORDS to single keystrokes to reply
Jul 17 '05 #15

P: n/a
WindAndWaves wrote:
"News Me" <newsTWOme@pacifierDOTcom> wrote in message
news:10*************@corp.supernews.com...

item with id 234
<input type='submit' ... name='submitted_id' value='add234'>
item with id 235
<input type='submit' ... name='submitted_id' value='add235'>
item with id 236
<input type='submit' ... name='submitted_id' value='add236'>
Then in your code:

$item_id = $HTTP_POST_VARS['submitted_id'];

$item_id will be one of 'add234', 'add235', or 'add236'.


Thank you for your reply. That is simpler than I thought. The only problem
I have now is that I don't want the text "add..." to appear on page. All I
want the user to see is "add to basket"....

Do you have any hints on this?

item with id 234
<input type='submit' ... name='submitted_id[234]' value='add to basket'>
item with id 235
<input type='submit' ... name='submitted_id[235]' value='add to basket'>
item with id 236
<input type='submit' ... name='submitted_id[236]' value='add to basket'>
$_POST['submitted_id'] is now an array with one element: the item id is
the key and the string 'add to basket' is the value.

Put the key in $item_id and continue with whatever is necessary.

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.