homepage Welcome to WebmasterWorld Guest from 54.205.188.59
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
validating user input from a dynamic checkbox and How to insert values
femiot




msg:4433587
 4:47 pm on Mar 26, 2012 (gmt 0)

Am new to php... I have been battling on how to validate my dynamic checkboxes in such a way that if none is checked the form is return, also I need to retain what was checked when the form postback due to other invalid inputs.


$result = mysql_query("SELECT * FROM course") or die(mysql_error());
if ($result)
{
while ($row = mysql_fetch_array($result)){
if (isset($_POST['courses']) and $_POST['courses'] == $row['cid']) {echo $row['cid'];}
print "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\">$row[cname]\n";
}
}


And as well I need to insert this values with last inserted id from another table into a new table.


$lastid=mysql_insert_id();
$sql2="INSERT INTO course_student (sno, cid,year, fmark)
VALUES ( '" . $lastid . "', '" . implode(", ",$_POST['courses']) . "', '" . date('Y') . "', '" . '100' . "')";
$result = mysql_query($sql2)
or die(mysql_error());

Please your help is all I need... Thanks in advance

 

LinusIT




msg:4433897
 12:50 pm on Mar 27, 2012 (gmt 0)

What errors (if any) are you getting?

From looking at your code I think you need to replace "and" to "&&" in your if isset statement.

rocknbil




msg:4433958
 3:51 pm on Mar 27, 2012 (gmt 0)

the semantic "and" and symbolic "&&" are synonymous.

if none is checked the form is return, also I need to retain what was checked when the form postback due to other invalid inputs.


Then you need to check for that FIRST before doing any database selections. A good way to do that, and to do it all in one place, is to run a function to validate the data. If there are no errors, the function will return null. If there are errors, the function would return a string. You can pass a list of required checkboxes to it.

Checkboxes are easiest of all. They will only be present in post/get if checked.

$required_checkboxes = Array ('one','two','three');
$errors = check_the_checkboxes($required_checkboxes);
//
if ($errors) {
// Re-output the form here. I'd use another function . . .
output_form($errors);
}
else {
// process normally
// output a success response
}
//
function check_the_checkboxes($checks) {
$errstring=null;
foreach ($checks as $input) {
if (! isset($_POST[$input])) {
$errstring .= "<li>The $input checkbox is required.</li>\n";
}
}
return $errstring;
}


This is not working code of course, and you'll have to do lots of other tihngs for existing form input types (text, select-one, etc.) but gives you a basis on how to work:

1. Cleanse input (not covered here)
2. Error check input
3a. If error, return to form with fileds intact
3b. If no error, process input
4. Return appropriate response.

femiot




msg:4434123
 9:19 pm on Mar 27, 2012 (gmt 0)

Thanks alot for your response. I have solved it!
$result = mysql_query("SELECT * FROM course") or die(mysql_error());
if ($result){
while ($row = mysql_fetch_array($result)){
$checked = 'unchecked';
if ($_POST['courses'] != "")
{
if (isset($_POST['courses'])){
if (in_array($row['cid'], $_POST['courses'])){
$checked = "checked";
}
}
}
echo "<input type=\"checkbox\" name=\"courses[]\" value=\"$row[cid]\" $checked >$row[cname]\n";
}
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved