Jump to content

Help with submitting form data


Dan06

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 '['

 

Link to comment
Share on other sites

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.

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.