Jump to content

Checkbox and insert issue


jahlen

Recommended Posts

Problem:  The form page consists of text inputs except for a series of checkboxes ($_POST['course']).  At least one of the checkboxes will be selected.  If more than one checkbox is selected, duplicate rows will be created based on the information from the text inputs.

 

The action page has the following code:

 

// Beginning

$as_course = $_POST['course'];

$as_startdate = $_POST['startdate'];

$tr_startdate = date("Y-m-d", strtotime($as_startdate));

$as_enddate = $_POST['enddate'];

$tr_enddate = date("Y-m-d", strtotime($as_enddate));

$as_site = addslashes($_POST['site']);

$tr_site = trim($as_site);

$as_siteurl = addslashes($_POST['site_url']);

$tr_siteurl = trim($as_siteurl);

$as_sitephone = addslashes($_POST['site_phone']);

$tr_sitephone = trim($as_sitephone);

$as_siteaddress = addslashes($_POST['site_address']);

$tr_siteaddress = trim($as_siteaddress);

$as_sitecity = addslashes($_POST['site_city']);

$tr_sitecity = trim($as_sitecity);

$as_sitestate = addslashes($_POST['site_state']);

$tr_sitestate = trim($as_sitestate);

$as_sitezip = addslashes($_POST['site_zip']);

$tr_sitezip = trim($as_sitezip);

$as_regdate = $_POST['reg_deadline'];

$tr_regdate = date("Y-m-d", strtotime($as_regdate));

$as_paydate = $_POST['pay_deadline'];

$tr_paydate = date("Y-m-d", strtotime($as_paydate));

$as_notes = addslashes($_POST['addnotes']);

$tr_notes = trim($as_notes);

 

$course = "";

foreach($_POST['course'] as $value)

{

$course .= $value . ", ";

}

// I'm lost here

 

Not sure how to complete the code and write the sql that would insert the row(s) to mysql database.

 

Thanks in advance to anyone who may help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/117908-checkbox-and-insert-issue/
Share on other sites

each checkbox needs to have a different name, or you are overwriting the value.

Example:

<html>
<form method="POST" action="?">
<table border="0">
	<tr>
		<td><input type="checkbox" name="test" value="1"></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="test" value="2"></td>
	</tr>
	<tr>
		<td><input type="submit" value="Submit"></td>
	</tr>
</table>
</form>
</html>
<?php
if (isset($_POST['test'])){
print_r($_POST);
}

run that, check both check boxes, and submit it. you will see

Array
(
    [test] => 2
)

Explanation: you are setting $_POST['test'] to "1", then resetting $_POST['test'], giving it a new value of "2".

Thanks jonsjava.

 

One thing I didn't include on my first post was that the checkboxes are generated by a query that will change as additional data is added to the table. 

 

That code is:

 

$c_query = "SELECT * FROM course";

$c_result = mysql_query($c_query) or die(mysql_error());

while($row = mysql_fetch_array($c_result))

{

$select_rad .= "<INPUT TYPE='checkbox' NAME='course[]' VALUE='$row[id]'> $row[c_title]<br>";

}

 

Now since I have the brackets following the name, shouldn't that allow me to process that as an array?

 

Sorry for my lack of knowledge and terminology.  You've probably figured it out that I'm a noob.

 

Thanks much.

Archived

This topic is now archived and is closed to further replies.

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