pcw Posted March 6, 2009 Share Posted March 6, 2009 Hi, I got this section of a script. I need to print the field name and type within table tags, but am getting an unexpected result: Here is the section of the script: <?php $optionCnt = 0; if ($_POST['addoption']) { echo '<form name="makeformfield" method="post"> Field Name: <input type="text" name="fieldname" value="'.$_POST['fieldname'].'"> Field Type: <select name="fieldtype"> <option>Text Field</option> <option>Text Area</option> <option>Radio Button</option> <option>Checkbox</option> <option>Select</option> </select><br />'; foreach($_POST as $field => $value) { if (substr($field,0,11) == "optionvalue") { $optionCnt++; echo "<input type='text' name='$field' value='$value' /><br />"; } } echo "<input type='text' name='optionvalue$optionCnt' value='' /><br />"; $ShowName = $_POST['hiddendata']."$name"; echo '<input type="hidden" name="hiddendata" value="'.$ShowName.'" />'; $ShowType= $_POST['hiddendata1']."$type"; echo '<input type="hidden" name="hiddendata1" value="'.$ShowType.'" />'; echo '<table border=0>'; foreach($_POST as $ShowName) { echo "<tr><td>Field Name: $ShowName</td>"; } foreach($_POST as $ShowType) { echo "<td>Field Type: $ShowType</td></tr>"; } echo stripslashes('<textarea cols="60" rows="5" name="previewarea" >'.$_POST['previewarea'].'</textarea>'); echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'; } ?> The result I get when submitting the form is: Field Name: first name Field Name: Text Field Field Name: Field Name: Field Name: update Field Type: first name Field Type: Text Field Field Type: Field Type: Field Type: update Instead of Field Name: First Name Field Type: Text Field Can anyone tell me how to fix this? Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/ Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 I have tried this instead, and am still getting the same sort of result. <?php echo '<p>view code:</p>'; $ShowName = $_POST['hiddendata']."$name"; echo '<input type="hidden" name="hiddendata" value="'.$ShowName.'" />'; $ShowType= $_POST['hiddendata1']."$type"; echo '<input type="hidden" name="hiddendata1" value="'.$ShowType.'" />'; echo '<table border=0>'; foreach($_POST as $name) { echo "<tr><td>Field Name: $ShowName</td><td>Field Type: $ShowType</td></tr>"; echo "</table>"; } ?> Can anybody tell me why it prints it more than once each time the form is updated? Thanks Thanks Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778817 Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi The first one is looping through all the input form fields and print them out with "Field Name" in front, and then doing it again with "Field Type" in front of them. Also a bit of an issue with table tags. Not 100% sure what you are trying to do. I think you are processing a form, and probably the generated one we were talking about yesterday. Can you generate the form, then do a "view source" and post the source of the form? My guess is that you want something like:- <?php echo '<p>view code:</p>'; echo '<table border=0>'; foreach($_POST as $field => $value) { if (substr($field,0,9) == "fieldname") { $fieldNumber = substr($field,9); echo '<input type="hidden" name="fieldname'.$fieldNumber.'" value="'.$_POST['fieldname'.$fieldNumber].'" />'; echo '<input type="hidden" name="fieldtype'.$fieldNumber.'" value="'.$_POST['fieldtype'.$fieldNumber].'" />'; echo "<tr><td>Field Name: ".$_POST['fieldname'.$fieldNumber]."</td><td>Field Type: ".$_POST['fieldtype'.$fieldNumber]."</td></tr>"; } } echo "</table>"; ?> This is looping round all the form fields, checking each to see if they start "fieldname", and if so grabbing the number from the end and putting it back out on the new form with the name and type, and also putting it out in a table. You will need to add code to that to put out the new fields you are adding to put the next number on the end of the field names. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778821 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 Hi Keith, I tried what you said, but it kept printing out the same data in the previewarea, and didnt state the form name and form field above. ie. it kept printing <input type="text" name="firstname"> <input type="text" name="firstname"> etc - in the previewarea I made a change to it and the results appear correctly in the previewarea, but it does not print the form name and form field above. This is what I got now: <?php $db = "moveitho_sitebuilder"; $dbuser = "moveitho_paul"; $dbpass = "test"; $optionCnt = 0; if ($_POST['addoption']) { echo '<form name="makeformfield" method="post"> Field Name: <input type="text" name="fieldname" value="'.$_POST['fieldname'].'"> Field Type: <select name="fieldtype"> <option>Text Field</option> <option>Text Area</option> <option>Radio Button</option> <option>Checkbox</option> <option>Select</option> </select><br />'; foreach($_POST as $field => $value) { if (substr($field,0,11) == "optionvalue") { $optionCnt++; echo "<input type='text' name='$field' value='$value' /><br />"; } } echo "<input type='text' name='optionvalue$optionCnt' value='' /><br />"; echo '<p>view code:</p>'; echo '<table border=0>'; foreach($_POST as $field => $value) { if (substr($field,0,9) == "fieldname") { $fieldNumber = substr($field,9); echo '<input type="hidden" name="hiddendata'.$ShowName.'" value="'.$_POST['hiddendata'.$ShowName].'" />'; echo '<input type="hidden" name="hiddendata1'.$ShowType.'" value="'.$_POST['hiddendata'.$ShowType].'" />'; echo "<tr><td>Field Name: ".$_POST['fieldname'.$ShowName]."</td><td>Field Type: ".$_POST['fieldtype'.$ShowType]."</td></tr>"; } } echo "</table>"; echo stripslashes('<textarea cols="60" rows="5" name="previewarea" >'.$_POST['previewarea'].'</textarea>'); echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'; } else { echo '<form name="makeformfield" method="post"> Field Name: <input type="text" name="fieldname"> Field Type: <select name="fieldtype"> <option>Text Field</option> <option>Text Area</option> <option>Radio Button</option> <option>Checkbox</option> <option>Select</option> </select><br />'; echo "<input type='text' name='optionvalue$optionCnt' value='' /><br />"; if ($_POST['update']) { $option = $_POST['field_options']; $type = $_POST['fieldtype']; $name = $_POST['fieldname']; if ($type == 'Text Field') { $NewField = '<input type="text" name="'.$name.'" />'; } if ($type == 'Text Area') { $NewField = '<textarea cols="5" rows="4" name="'.$name.'"></textarea>'; } if ($type == 'Radio Button') { $NewField = '<input type="radio" name="'.$name.'" />'; } if ($type == 'Checkbox') { $NewField = '<input type="checkbox" name="'.$name.'" />'; } if ($type == 'Select') { $NewField = '<select name="'.$name.'" />'.chr(13); foreach($_POST as $field => $value) { if (substr($field,0,11) == "optionvalue") { $NewField .= '<option>'.$value.'</option>'.chr(13); } } $NewField .= '</select>'; } echo '<p>view code:</p>'; echo '<table border=0>'; foreach($_POST as $field => $value) { if (substr($field,0,9) == "fieldname") { $fieldNumber = substr($field,9); echo '<input type="hidden" name="fieldname'.$fieldNumber.'" value="'.$_POST['fieldname'.$fieldNumber].'" />'; echo '<input type="hidden" name="fieldtype'.$fieldNumber.'" value="'.$_POST['fieldtype'.$fieldNumber].'" />'; echo "<tr><td>Field Name: ".$_POST['fieldname'.$fieldNumber]."</td><td>Field Type: ".$_POST['fieldtype'.$fieldNumber]."</td></tr>"; } } echo "</table>"; echo stripslashes('<textarea cols="60" rows="5" name="previewarea" >'.$_POST['previewarea'].chr(13).$NewField.'</textarea>'); echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'; } else { echo stripslashes('<textarea cols="60" rows="5" name="previewarea" ></textarea>'); echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'; } } echo '</form>'; if ($_POST['submit']) { $ShowName = $_POST['hiddendata']."Field Name: $name"; echo '<input type="hidden" name="hiddendata" value="'.$ShowName.'" />'; $ShowType= $_POST['hiddendata1']."Field Type: $type"; echo '<input type="hidden" name="hiddendata1" value="'.$ShowType.'" />'; $write = stripslashes(''.$_POST['previewarea'].chr(13).$NewField.''); $filename = "data/fields.txt"; $fa = fopen( $filename, "w" ) or die("Error opening $filename"); fwrite( $fa, "$write" ); fclose( $fa ); $link = mysql_pconnect( "localhost", $dbuser, $dbpass ); if ( ! $link ) { $dberror = mysql_error(); return false; } if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } $query = "INSERT INTO FormFields ( name, type ) values('$ShowName', '$ShowType')"; if ( ! mysql_query( $query, $link ) ) { $dberror = mysql_error(); return false; } return true; }; ?> Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778829 Share on other sites More sharing options...
alphanumetrix Posted March 7, 2009 Share Posted March 7, 2009 not sure what you're trying to do. why do you need a loop? Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778837 Share on other sites More sharing options...
kickstart Posted March 7, 2009 Share Posted March 7, 2009 Hi Think this is what you want:- <?php $db = "moveitho_sitebuilder"; $dbuser = "moveitho_paul"; $dbpass = "test"; global $vbCrLf; $vbCrLf = chr(13).chr(10); $optionCnt = 0; if ($_POST['addoption']) { echo '<form name="makeformfield" method="post"> Field Name: <input type="text" name="fieldname" value="'.$_POST['fieldname'].'"> Field Type: <select name="fieldtype"> <option>Text Field</option> <option>Text Area</option> <option>Radio Button</option> <option>Checkbox</option> <option>Select</option> </select><br />'.$vbCrLf; foreach($_POST as $field => $value) { if (substr($field,0,11) == "optionvalue") { $optionCnt++; echo "<input type='text' name='$field' value='$value' /><br />".$vbCrLf; } } echo "<input type='text' name='optionvalue$optionCnt' value='' /><br />".$vbCrLf; echo '<p>view code:</p>'; echo '<table border=0>'; $fieldCount = 0; foreach($_POST as $field => $value) { if (substr($field,0,14) == "fieldnamestore") { $fieldNumber = substr($field,14); echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$_POST['fieldnamestore'.$fieldNumber].'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="'.$_POST['fieldtypestore'.$fieldNumber].'" />'.$vbCrLf; echo "<tr><td>Field Name: ".$_POST['fieldnamestore'.$fieldNumber]."</td><td>Field Type: ".$_POST['fieldtypestore'.$fieldNumber]."</td></tr>".$vbCrLf; $fieldCount++; } } echo "</table>"; echo stripslashes('<textarea cols="60" rows="5" name="previewarea" >'.$_POST['previewarea'].'</textarea>'); echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'; } else { echo '<form name="makeformfield" method="post"> Field Name: <input type="text" name="fieldname"> Field Type: <select name="fieldtype"> <option>Text Field</option> <option>Text Area</option> <option>Radio Button</option> <option>Checkbox</option> <option>Select</option> </select><br />'.$vbCrLf; echo "<input type='text' name='optionvalue$optionCnt' value='' /><br />".$vbCrLf; if ($_POST['update']) { echo '<p>view code:</p>'; echo '<table border=0>'; $fieldCount = 0; foreach($_POST as $field => $value) { if (substr($field,0,14) == "fieldnamestore") { $fieldNumber = substr($field,14); echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$_POST['fieldnamestore'.$fieldNumber].'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="'.$_POST['fieldtypestore'.$fieldNumber].'" />'.$vbCrLf; echo "<tr><td>Field Name: ".$_POST['fieldnamestore'.$fieldNumber]."</td><td>Field Type: ".$_POST['fieldtypestore'.$fieldNumber]."</td></tr>".$vbCrLf; $fieldCount++; } } $option = $_POST['field_options']; $type = $_POST['fieldtype']; $name = $_POST['fieldname']; if ($type == 'Text Field') { $NewField = '<input type="text" name="'.$name.'" />'; echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$name.'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="Text Field" />'.$vbCrLf; echo "<tr><td>Field Name: $name</td><td>Field Type: Text Field</td></tr>".$vbCrLf; } if ($type == 'Text Area') { $NewField = '<textarea cols="5" rows="4" name="'.$name.'"></textarea>'; echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$name.'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="Text Area" />'.$vbCrLf; echo "<tr><td>Field Name: $name</td><td>Field Type: Text Area</td></tr>".$vbCrLf; } if ($type == 'Radio Button') { $NewField = '<input type="radio" name="'.$name.'" />'; echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$name.'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="Radio Button" />'.$vbCrLf; echo "<tr><td>Field Name: $name</td><td>Field Type: Radio Button</td></tr>".$vbCrLf; } if ($type == 'Checkbox') { $NewField = '<input type="checkbox" name="'.$name.'" />'; echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$name.'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="Checkbox" />'.$vbCrLf; echo "<tr><td>Field Name: $name</td><td>Field Type: Checkbox/td></tr>".$vbCrLf; } if ($type == 'Select') { $NewField = '<select name="'.$name.'" />'.chr(13); foreach($_POST as $field => $value) { if (substr($field,0,11) == "optionvalue") { $NewField .= '<option>'.$value.'</option>'.chr(13); } } $NewField .= '</select>'; echo '<input type="hidden" name="fieldnamestore'.$fieldCount.'" value="'.$name.'" />'.$vbCrLf; echo '<input type="hidden" name="fieldtypestore'.$fieldCount.'" value="Select" />'.$vbCrLf; echo "<tr><td>Field Name: $name</td><td>Field Type: Select</td></tr>".$vbCrLf; } echo "</table>"; echo stripslashes('<textarea cols="60" rows="5" name="previewarea" >'.$_POST['previewarea'].chr(13).$NewField.'</textarea>').$vbCrLf; echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'.$vbCrLf; } else { echo stripslashes('<textarea cols="60" rows="5" name="previewarea" ></textarea>').$vbCrLf; echo '<br /><input type="submit" name="update" value="update"><input type="submit" name="submit" value="submit"> <input type="submit" name="addoption" value="addoption">'.$vbCrLf ; } } echo '</form>'; ?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778847 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 Hi Keith, thanks again, that is just what I needed. I am just confused as to what the variables for the separate field name and field type are. Whereas I was using ShowName and ShowType to print the results in the database, what variables would I use now? Thanks Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778937 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 I have tried using this to add the results to the database, but its not adding anything. If I change the variables, it just adds blank fields. Any ideas? <?php $link = mysql_pconnect( "localhost", $dbuser, $dbpass ); if ( ! $link ) { $dberror = mysql_error(); return false; } if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } $query = "INSERT INTO FormFields ( name, type ) values('$name', '$vbCrLf').$vbCrLf"; if ( ! mysql_query( $query, $link ) ) { $dberror = mysql_error(); return false; } return true; }; ?> Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778961 Share on other sites More sharing options...
redarrow Posted March 7, 2009 Share Posted March 7, 2009 try this if it dosent work take the post away from $res. <?php $link = mysql_pconnect( "localhost", $dbuser, $dbpass ); if ( ! $link ) { $dberror = mysql_error(); return false; } if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } $res=$_POST['res']; $res=$vbCrLf.$vbCrLf; $query = "INSERT INTO FormFields ( name, type ) values('$name', '$res'"; if ( ! mysql_query( $query, $link ) ) { $dberror = mysql_error(); return false; } return true; }; ?> Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778966 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 Hi, thanks for your reply, but this just adds blank fields to the database Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-778974 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 Anyone know how I can print the results to the db? I am having no luck with doing so Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-779036 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 Would this be the right sort of thing? <?php $fname = "'.$_POST['fieldnamestore'.$fieldNumber].'".$vbCrLf; $ftype = "'.$_POST['fieldtypestore'.$fieldNumber].'".$vbCrLf; ?> I tried this but get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/moveitho/public_html/sitebuilder/testform.php on line 151 I am doing the right sort of thing? and how do I fix this? Thanks Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-779074 Share on other sites More sharing options...
pcw Posted March 7, 2009 Author Share Posted March 7, 2009 ok, i got this: $fname = $_POST['fieldnamestore'.$fieldNumber].$vbCrLf; $ftype = $_POST['fieldtypestore'.$fieldNumber].$vbCrLf; But it is still not printing the results to the database. What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-779126 Share on other sites More sharing options...
pcw Posted March 8, 2009 Author Share Posted March 8, 2009 Anyone got a solution for this? Thanks Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-779482 Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Firstly $vbCrLf does not need to go to the database. That is purely a carraige return and line feed, put out with the HTML to make the generated code easier to read. The next thing is that while the code copes with multiple input fields, the insert code at the end didn't. You need to loop through the input fields and insert all of them. I have had a play, and think this fragment will do for the end bit. Might be some errors (not test run it) but hope it will give you the right idea. I have left the return statements in assuming that the stuff to declare it as a function is elsewhere. if ($_POST['submit']) { $write = stripslashes(''.$_POST['previewarea'].chr(13).$NewField.''); $filename = "data/fields.txt"; $fa = fopen( $filename, "w" ) or die("Error opening $filename"); fwrite( $fa, "$write" ); fclose( $fa ); $link = mysql_pconnect( "localhost", $dbuser, $dbpass ); if ( ! $link ) { $dberror = mysql_error(); return false; } else { if ( ! mysql_select_db( $db, $link ) ) { $dberror = mysql_error(); return false; } else { foreach($_POST as $field => $value) { if (substr($field,0,14) == "fieldnamestore") { $fieldNumber = substr($field,14); $ShowName = $_POST['fieldnamestore'.$fieldNumber]; $ShowType = $_POST['fieldtypestore'.$fieldNumber]; $query = "INSERT INTO FormFields ( name, type ) values('$ShowName', '$ShowType')"; if ( ! mysql_query( $query, $link ) ) { $dberror = mysql_error(); return false; } } } } } return true; } All the best Keith Link to comment https://forums.phpfreaks.com/topic/148296-printing-result-help-foreach-maybe/#findComment-779488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.