Jump to content

foreach using form field


Canman2005

Recommended Posts

Hi all

 

Wonder if anyone can help.

 

Basically I have a bunch of form fields which look like

 

<input name="fullname" type="text">
<input name="1-value[]" type="text" value="Apple">
<input name="4-value[]" type="text" value="Microsoft">
<input name="6-value[]" type="text" value="IBM">
<input name="12-value[]" type="text" value="1to1">

 

sometimes there is just 1 field, sometimes 4 fields and sometimes 20 or so fields.

 

what I want to do is run an

 

INSERT INTO

 

for each form field that exists that has "-value[]" set as its "name=" tag

 

but also, I want to be able to extract 2 things, firstly the numeric value which is defined before the "-value[]" and also the actual value of each form field (ie: IBM \ Apple).

 

This means I would then be able to do something like

 

INSERT INTO `items` SET `idnumber`="[b]NUMERIC VALUE[/b]", `value`="[b]FIELD VALUE (ie: IBM \ Apple)[/b]"

 

for each form field returned.

 

Can anyone advise?

 

Thanks very much

Link to comment
https://forums.phpfreaks.com/topic/189413-foreach-using-form-field/
Share on other sites

Why don't you do this (note the input name):

 

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="value[4]" value="Apple" />
<input type="submit" name="submit" value="Submit" />
</form>

 

then you can do something like this:

foreach($_POST['value'] as $key => $val){
mysql_query("INSERT INTO `items` SET `idnumber`='$key', `value`='$val'");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.