Dan06 Posted October 2, 2008 Share Posted October 2, 2008 My form values are not being added to the $_POST[] array, when selected, and thus the $_POST[] array is continually empty/null. Can anyone tell me why the input variables are not being added to the $_POST[] array? Here is code for my form: <table> <form action="<?php echo $editFormAction; ?>" id="TypeSpecForm" name="TypeSpecForm" method="post"> <tr> <td><div id="ProdTbl"> <p><strong>Which Type of Products?</strong></p> <table border="1" align="center"> <?php do { ?> <tr> <td> <?php echo '<input type="checkbox"' . " name=" . $row_ProdType['TypeName'] . " id=" . $row_ProdType['TypeName'] . " value=" . $row_ProdType['TypeName'] . '/>' ?> </td> <td><?php echo $row_ProdType['TypeName']; ?></td> </tr> <?php } while ($row_ProdType = mysql_fetch_assoc($ProdType)); ?> </form> </table> </div> Below is the code for updating the target table (I've already checked the GetSQLValueString function and it is working correctly): if ((isset($_POST["SubmitList"])) && ($_POST["SubmitList"] == "TypeSpecForm")) { $updateSQL = sprintf("UPDATE businessprofile SET ProductListing=%s, ServiceListing=%s WHERE Id=" . "'" . $_SESSION['Reg'] . "'", GetSQLValueString($_POST[$row_ProdType['TypeName']], "text"), GetSQLValueString($_POST[$row_ServType['TypeName']], "text")); mysql_select_db($database_dbConnection, $dbConnection); $Result1 = mysql_query($updateSQL, $dbConnection) or die("Query error in UPDATE query! Query text: $updateSQL<br>Error:" . mysql_error()); $updateGoTo = "securityregister.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Maybe the problem is with your dynamically created input names. View the source of your page to see if the names are being correctly generated. If they are, add print_r($_POST); to the top of your action page to see all the $_POST values. Also check for duplicate input names. If you name all the checkboxes the same, you'll only get one result back. Quote Link to comment Share on other sites More sharing options...
ratherbecoding Posted October 2, 2008 Share Posted October 2, 2008 if checkboxes are named: name="bunchofboxes[]" they will return as an array. Quote Link to comment Share on other sites More sharing options...
Dan06 Posted October 2, 2008 Author Share Posted October 2, 2008 I've done the "view the source" of my page and the form elements are being built correctly each input type has a unique name and id, for example here is a part of the view - <td> <input type='checkbox' name='Computer Software' id='Computer Software' value='Computer Software'/> </td> <td>Computer Software</td> </tr> <tr> <td> <input type='checkbox' name='Electronics' id='Electronics' value='Electronics'/> </td> <td>Electronics</td> </tr> I've also done the print_r($_POST) and this is what I get: Array ( [Computer_Software] => Computer Software [Electronics] => Electronics [listingSave] => Save [submitList] => TypeSpecForm ) Computer Software and Electronics are the two selections made. Thus the $_POST array DOES contain the selected inputy variable (checkbox) at this point, but when I try to update the target table with the appropriate $_POST array values - there are no values, it is empty. I can't figure out why. Quote Link to comment Share on other sites More sharing options...
revraz Posted October 2, 2008 Share Posted October 2, 2008 Where is your Submit action? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Try echoing your query. Also, why aren't you using the $_POST actual key, rather than the variable as the key? Quote Link to comment Share on other sites More sharing options...
Dan06 Posted October 2, 2008 Author Share Posted October 2, 2008 The echo $updateSQL result, when checking computer software and electronics checkboxs, is: UPDATE businessprofile SET ProductListing=NULL, ServiceListing=NULL WHERE Id='m7h16v' The NULL values are because the GetSQLValueString function places NULL values in place of empty data or "". Which when taken inconjunction with the print_r($_POST) information means that the problem is in the update code, because the print_r($_POST) shows there are values in the $_POST array. As far as using the $_POST actual key rather than the variable key, I thought I was. Isn't the actual key based on the name/id of the input variable? And in my dynamic form that is generated by $row_ProdType['TypeName'], so I placed that in the $_POST[...]. I posted the submitting action/code in my earlier post, but here it is again: if ((isset($_POST["SubmitList"])) && ($_POST["SubmitList"] == "TypeSpecForm")) { $updateSQL = sprintf("UPDATE businessprofile SET ProductListing=%s, ServiceListing=%s WHERE Id=" . "'" . $_SESSION['Reg'] . "'", GetSQLValueString($_POST[$row_ProdType['TypeName']], "text"), GetSQLValueString($_POST[$row_ServType['TypeName']], "text")); mysql_select_db($database_dbConnection, $dbConnection); $Result1 = mysql_query($updateSQL, $dbConnection) or die("Query error in UPDATE query! Query text: $updateSQL<br>Error:" . mysql_error()); $updateGoTo = "securityregister.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } Quote Link to comment Share on other sites More sharing options...
revraz Posted October 2, 2008 Share Posted October 2, 2008 I mean on your FORM, where do you actually SUBMIT it? You are checking $_POST["SubmitList"] but where do you set that? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Replace: GetSQLValueString($_POST[$row_ProdType['TypeName']] with: GetSQLValueString($_POST['Computer_Software'] Quote Link to comment Share on other sites More sharing options...
Dan06 Posted October 2, 2008 Author Share Posted October 2, 2008 With the replaced code the update query now returns: UPDATE businessprofile SET ProductListing='Computer Software', ServiceListing=NULL WHERE Id='m7h16v'. The code works when Computer Software is selected. However, the problem is still present when I don't choose Computer Software; if I choose any other option say Electronics - the update query returns: UPDATE businessprofile SET ProductListing=NULL, ServiceListing=NULL WHERE Id='m7h16v' The form code for submit is below: <input name="ListingSave" type="submit" id="ListingSave" value="Save"/> </p> </center></p><input name="SubmitList" type="hidden" id="SubmitList" value="TypeSpecForm" /></td> </tr> </form> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Do you want multiple values to be checked? If so, see below. If not, you need to use radio buttons. Are all these values supposed to be inserted into the same place? If so, you need to rename all of your checkboxes the same thing and add [] to the end. Then loop through all the selected values and insert/update them. Quote Link to comment Share on other sites More sharing options...
Dan06 Posted October 2, 2008 Author Share Posted October 2, 2008 Yes, I want multiple values to be checked. And yes, all these values are supposed to be updated in the same place. For example, the current form as 3 checkboxs - Computer Hardware, Computer Software, Electronics. I want the user to check one of them, two of them, or all of them, and have the selections updated in the same table colum. Now as for renaming the checkboxes the same thing and adding [] that is a bit more difficult. Right now the form is created from stored data in a table. The form pulls this data and loops through $_rowProdType['TypeName'] creating checkboxs and their appropriate label. How can I rename them, if I wan the table to be dynamically created? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 You just need to change this: <?php echo '<input type="checkbox"' . " name=" . $row_ProdType['TypeName'] . " id=" . $row_ProdType['TypeName'] . " value=" . $row_ProdType['TypeName'] . '/>' with this: <?php echo '<input type="checkbox"' . " name="prods[]" id=" . $row_ProdType['TypeName'] . " value=" . $row_ProdType['TypeName'] . '/>' Then, your $_POST['prods'] variable will be an array of all the checked values. Quote Link to comment Share on other sites More sharing options...
Dan06 Posted October 2, 2008 Author Share Posted October 2, 2008 I made the change to: <?php echo "<input type='checkbox'" . " name= Products[]" . " id=" . "'" . $row_ProdType['TypeName'] . "'" . " value=" . "'" . $row_ProdType['TypeName'] . "'" . "/>" ?> The update query and print_r($_POST) returns, when Computer Software and Electronics are checked: UPDATE businessprofile SET ProductListing=NULL, ServiceListing=NULL WHERE Id='m7h16v' Array ( [Products] => Array ( [0] => Computer Software [1] => Electronics ) [listingSave] => Save [submitList] => TypeSpecForm ) It's still giving me null values... could the quotes around the Products[] array be an issue? When I remove the Products[] from quotes I get an error stating: Unexpected '[' Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 That's because you can't just insert an array like that. You need to use the foreach loop to loop through all those values. But, I see another problem with your code. If you do this, it will overwrite the previous value. In other words, if you loop through and do the update, it will update to one value, then overwrite that value with the next, and so on. You need to either be updating with a unique ID for each one, or change it to an insert if you want multiple values. Quote Link to comment 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.