Jump to content

Find name of form input?


lfernando

Recommended Posts

I have a string that contains dropdown menus, when displayed on the browser it looks like a form:

 

<form action=index.php method=post>
<select name=test_1_here><option>pass</option><option>fail</option></select> first item
<select name=test_44_here><option>pass</option><option>fail</option></select> second item
<select name=test_23_here><option>pass</option><option>fail</option></select> third item
<input type=Submit></form>

 

The user hits submit.

I need my code to find out the value (pass/fail) of each item.

I know how many items there will be but not their name (just that they'll start with "test_" and end with "_here")

 

This is the code I have:

 

for ($count = 1; $count <= 3; $count++) // do this for as many items (i know this number)
{
$id="test_".???."_here";
$value=$_POST[$id];
$sql= 'INSERT INTO table SET id="'.$id.'", value="'.$value.'"';
if (!mysqli_query($link, $sql)) { echo 'Error updating with new automation request: ' . mysqli_error($link); exit(); }   
}

 

I need to know how to find out the ???? in the middle, i figured i need REG EX?

 

Thanks everyone!!

Link to comment
https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/
Share on other sites

Hi Dan. Yes I see how it's easier, and moving forward i'll definetly do it that way. But I have a large amount of data with this format already, which is what I need to work with for now. So any suggestions on how to process fields named test_???_here would be much appretiated!

<?php
foreach ($_POST as $id => $value) {
if (preg_match('/^test_[0-9]+_here$/',$id)){
	$sql= 'INSERT INTO table SET id="'.$id.'", value="'.$value.'"';
	if (!mysqli_query($link, $sql)) echo 'Error updating with new automation request: ' . mysqli_error($link); exit(); 
}
}
?>

I have a large amount of data with this format already

 

Since you are just now getting around to processing that data, there's no point to NOT fixing the format, assuming you are dynamically producing this large amount of data forms using php code. You haven't hard coded these by writing out the HTML for each each one have you?

 

If your select field names are already stored some where (array, flat-file, database) using the id portion of the name, you can save yourself a lot of work by having php produce your forms, using the suggested arrays.

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.