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
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!

Link to comment
Share on other sites

<?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(); 
}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.