lfernando Posted January 17, 2011 Share Posted January 17, 2011 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!! Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/ Share on other sites More sharing options...
requinix Posted January 17, 2011 Share Posted January 17, 2011 </pre> <form action="index.php" method="post"> passfail first item passfail second item passfail third item < foreach ($_POST["test"] as $test => $status) { echo "Test {$test} has {$status}ed \n"; } Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1161029 Share on other sites More sharing options...
lfernando Posted January 24, 2011 Author Share Posted January 24, 2011 This is great, but my field names have text after the number, like in the example above. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164621 Share on other sites More sharing options...
ManiacDan Posted January 24, 2011 Share Posted January 24, 2011 Then don't do it that way, do it requinix's way. See how it works and is easier? -Dan Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164652 Share on other sites More sharing options...
lfernando Posted January 24, 2011 Author Share Posted January 24, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164700 Share on other sites More sharing options...
ManiacDan Posted January 24, 2011 Share Posted January 24, 2011 It will be more painful to write the PHP than to change all your filed names. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164748 Share on other sites More sharing options...
sasa Posted January 25, 2011 Share Posted January 25, 2011 <?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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164972 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2011 Share Posted January 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224765-find-name-of-form-input/#findComment-1164979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.