update radio button values without submit button - php

I'm having problem to update radio values. When I use a submit button to submit the form, it works fine, but when I use javascript, the values are not updated in database. And I'm using jquery mobile. Can anybody help? thanks!
UPDATED CODE, now only the first 3 radio buttons are working
dynamically generates 3 radio buttons for each task:
<?php
include 'connection.php';
$query = "SELECT*FROM plan";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$id=mysql_result($result, $i, "id");
$task=mysql_result($result, $i, "task");
$state=mysql_result($result, $i, "state");
?>
<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
<input type="hidden" name="id" value="<? echo "$id";?>">
<input type="radio" name="r" id="rA" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA"> </label>
<input type="radio" name="r" id="rB" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB"> </label>
<input type="radio" name="r" id="rC" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC"> </label>
<div data-role="collapsible" name="ud_c" value=" <?echo "$task";?> "><h3><?echo "$task";?></h3></div>
</form>
<?php
$i++;
}
?>
javascript which should submit the form on check of a radio button:
$(document).ready(function() {
$('input[type="radio"]').click(function(){
if ($(this).is(":checked"))
$('form#formnobtn').submit();
});
})
update database:
<?php
$id = $_POST['id'];
if (isset($_POST['r'])){
$state = $_POST['r'];
echo $state;
include 'connection.php';
$query= "UPDATE plan SET state='$state' WHERE id='$id'";
mysql_query($query);
mysql_close();
header('Location: nobtn.php');
}
?>

Please try this:-
I have created a hidden value for radio button checked value. It will send radio button checked value on form post. I dont know why you used id here that's why I am creating a $radio_checked_id variable in your php code.I hope this will help you.
dynamically generates 3 radio buttons for each task:
<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
<input type="hidden" name="id" value="<?php echo $id =(isset($id)) ? $id : '';?>">
<input type="radio" name="r" id="rA" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA"> </label>
<input type="radio" name="r" id="rB" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB"> </label>
<input type="radio" name="r" id="rC" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC"> </label>
<div data-role="collapsible" name="ud_c" value="<?php echo $task =(isset($task)) ? $task : '';?>"><h3><?php echo $task =(isset($task)) ? $task : '';?></h3></div>
<input type="hidden" name="checked_id" id="checked_id" value="">
</form>
javascript which should submit the form on check of a radio button:
$(document).ready(function() {
$('input[type="radio"]').click(function(){
var checked_radio_id = $(this).val();
$('#checked_id').val(checked_radio_id);
}
$('form#formnobtn').submit();
});
})
update database:
<?php
$submit = $_POST['submit'];
$id = $_POST['id'];
$radio_checked_id = $_POST['checked_id'];
if($submit){
if (isset($_POST['r'])){
$state = $_POST['r'];
echo $state;
include 'connection.php';
$query= "UPDATE plan SET state='$state' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
header('Location: nobtn.php');
}
else {
echo "Please select a radio button!";
}
}
?>
I'm not sure if the following line is right, 'submit' is actually the name of submit button when there is a submit button, but I have no idea how to correct it..
$submit = $_POST['submit'];

well, with radio buttons you do not need to add the id to the all, just make sure the names are the same.
something like this :
<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
<input type="radio" name="r" id="id" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA"> </label>
<input type="radio" name="r" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB"> </label>
<input type="radio" name="r" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC"> </label>
</form>
So just remove the hidden input, and in javascript:
$('input[type="radio"]').change(e=>{
$('form').submit();
})
Another option is to change the radio buttons into regular buttons and create an active class to show the selected CSS something like:
<form ....>
<input type="hidden" name="id" data-group="x" value="<? echo "$id";?>">
<button <?php if ($statE == 'A'): ?>class='active'<?php endif; ?> data-value="A" data-group="x">A</button>
<button <?php if ($statE == 'B'): ?>class='active'<?php endif; ?> data-value="B" data-group="x">B</button>
<button <?php if ($statE == 'C'): ?>class='active'<?php endif; ?> data-value="C" data-group="x">C</button>
</form>
and on submit:
$('button[data-value]').click(e=>{
e.preventDefault();
$(`input[data-group="${e.target.dataset.group}"]`).val(e.target.dataset.group);
$('form').submit();
})

Related

How i can get value from radio button form in php?

Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>

Setting a radio button into session to retain checked state

I am trying to store the input value of a radio button and storing that intoa session so that if the user roams around the site the radio remains checked unless they switch it themselves and then the new selection remains checked.
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
<?php
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
if(isset($reportType)){
} else{
$reportType = 1;
}
?>
I cannot seem to to get it to remain in a checked state...
Put the value in session and use the session variable to populate value in radio button in place of using extra variable.
By populating from session It will help to retain in all pages.
<?php
$_SESSION['reportType'] = $_POST['reportType'];
?>
<form action="" method="POST" id="reportSwitch">
<input type="radio" name="reportType" id="leadership" value="1" <?php if($_SESSION['reportType'] == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($_SESSION['reportType'] == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
check this code
<?php
session_start();
$_POST['reportType'] = 1; // for testing it is set define value , you can change
if(isset($_POST['reportType'])){
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
} else {
$reportType = $_SESSION['reportType'];
}
if(!isset($reportType)){
$reportType = 1;
}
?>
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>

How to update the database if I change the dropdown list element and checkbox value

I have created one form in that I have put one drop down list and group of checkbox.
If user select any item from that drop down list than particular item should updated in database(Ex.first user has select division A from drop down list than the A should enter into database and if after some time same user change division from A to B than in database in place of A data should B when user press update button.
In the group of checkbox if user change (select or deselect) checkbox compare to previous input than respective checkbox value should updated in database.(Ex. If user has checked two checkbox than the particular checkbox value should update in database and after some time same user has checked all three checkbox or only one checkbox and particular change should update in database when user click on update button
Here I have put my code. If anyone know solution than please help.
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db_new", $connection);
$query=mysql_query("SELECT * FROM student_info where id=17");
$row= mysql_fetch_row($query);
$rollno = $row[1];
$name = $row[2];
$division= $row[3];
$std = $row[4];
$gender = $row[5];
$subject=mysql_query("select * from student_info where id=17");
while($fetch=mysql_fetch_object($subject))
{
$r=$fetch->subject;
$sub=explode(",",$r);
}
if(count($_GET)>0){
$rollno=$_GET['rollno'];
$name=$_GET['name'];
$std=$_GET['std'];
$gender=$_GET['gender'];
$update=mysql_query("update student_info set roll_no='$rollno', name='$name',
std='$std', gender='$gender' where id='17'",$connection);
}
mysql_close($connection);
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Form</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery%20validate/jquery.validate.min.js"></script>
<script src="jquery%20validate/additional-methods.min.js"></script>
<script src="form.js"></script>
</head>
<body>
<form id="editform" method="GET" action="task.php">
<div>
<h1>Form</h1>
<label><b>Roll No:</b></label>
<input id="rollno" name="rollno" type="text" value="<?php echo $rollno ?>"> <br><br>
<label><b>Name:</b></label>
<input id="name" name="name" type="text" value="<?php echo $name ?>"> <br><br>
<label><b>Division:</b></label>
<select id="division">
<option value=" " <?php if (!empty($division) && $division == ' ') echo 'selected = "selected"'; ?>>--select--</option>
<option value="A" <?php if (!empty($division) && $division == 'A') echo 'selected = "selected"'; ?>>A</option>
<option value="B" <?php if (!empty($division) && $division == 'B') echo 'selected = "selected"'; ?>>B</option>
<option value="C" <?php if (!empty($division) && $division == 'C') echo 'selected = "selected"'; ?>>C</option>
<option value="D" <?php if (!empty($division) && $division == 'D') echo 'selected = "selected"'; ?>>D</option>
</select><br><br>
<label><b>Std:</b></label>
<input id="std" name="std" type="text" value="<?php echo $std ?>"><br><br>
<label><b>Gender:</b></label>
<input type="radio" name="gender" value="male" <?php if($gender=='male'){ echo "checked=checked";}?>>Male
<input type="radio" name="gender" value="female" <?php if($gender=='female'){ echo "checked=checked";}?>>Female<br><br>
<label><b>Subject:</b></label><br>
<input type="checkbox" name="box[]" class="subject" value="maths" <?php if (in_array("maths", $sub)){echo "checked";}?>>maths<br>
<input type="checkbox" name="box[]" class="subject" value="science" <?php if (in_array("science", $sub)){echo "checked";}?>>science<br>
<input type="checkbox" name="box[]" class="subject" value="english" <?php if (in_array("english", $sub)){echo "checked";}?>>english<br>
<input id="submit" type="submit" value="submit">
<input id="update" type="submit" value="update">
</div>
</form>
</body>
</html>
You can use jquery ajax to update the data on select and on change of dropdown.
$.post('page.php',{parameter:value},function(data){
//operation performed after insertion
});

Dynamic radio button with self-post using PHP & MySQL

I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />

PHP Form Processing Order precedence

Am new to the world of development and am just starting to pick up PHP. I have basic form that attempts to validate the checkboxes the user has selected or checked. My code is below. The question I have is why is that when I have the order of my form as follows, the form does not pass the value NET, PHP or RUBY and the values that are costantly passed are no.
--- Form code that does not work ---
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="hidden" name="ch3" value="no">
<input type="submit" name="submit" value="submit">
However if my code is as follows;
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch3" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
The boxes appear checked. The entire code below.
<?php
$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
if($_POST["ch1"] == "net") {
$ch1status = "checked";
}
}
if(isset($_POST["ch2"])) {
if($_POST["ch2"] == "php") {
$ch2status = "checked";
}
}
if(isset($_POST["ch3"])) {
if($_POST["ch3"] == "ruby") {
$ch3status = "checked";
}
}
if ($_POST["ch1"] == "no" && $_POST["ch2"] == "no" && $_POST["ch3"] == "no") {
print "There is no such choice";
}
}
?>
<html>
<head>
<title>Sample form checkbxoes</title>
</head>
<body>
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="hidden" name="ch1" value="no">
<input type="checkbox" name="ch1" value="net" <?php print $ch1status ?>>.NET
<input type="hidden" name="ch2" value="no">
<input type="checkbox" name="ch2" value="php" <?php print $ch2status ?>>PHP
<input type="hidden" name="ch3" value="no">
<input type="checkbox" name="ch3" value="ruby" <?php print $ch3status ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
print $_POST["ch1"];
print $ch1status;
}
if(isset($_POST["ch2"])) {
print $_POST["ch2"];
print $ch2status;
}
if(isset($_POST["ch3"])) {
print $_POST["ch3"];
print $ch3status;
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
</body>
</html>
</form>
Also is there any other way of validating if the user has not selected any checkboxes as opposed to using hidden form fields.
Its just a browser-issue and its quite simple: The elements have the same name and the later element overwrites the first one.
Another way of validating, if a checkbox is not checked is to check, if its set in the $POST-array. If its missing, its treated like "not checked".
UNDEFINED INDEXES:
This is because checkboxes are only sent if they are checked. One thing you can do is always check the variable with isset (e.g. isset($_POST['ch1'])) before using them; another is to name your checkboxes the same thing with a [] following the name (e.g. name="languages[]") and then do something like this:
// Create a list of languages that are OK (remember, some users are malicious)
$languages = array('net','php','ruby');
// Compile a list of the answers the user picked; force it to be an
// array by either explicitly casting to an array, or using an empty array
// if none chosen
$picked = isset($_POST['languages']) ? (array)$_POST['languages'] : array();
// first, use array_intersect to remove entries present in one and not the other
// i.e. invalid entries from the client or entries not picked from the whole list
// then, "flip" the array so that the values become keys,
// because isset is faster than in_array
$valid_langs = array_flip(array_intersect($languages, $picked));
// check on languages
if (isset($valid_langs['php'])) { /* PHP picked */ }
if (isset($valid_langs['net'])) { /* NET picked */ }
if (isset($valid_langs['ruby'])) { /* Ruby picked */ }
Simpler Solution:
<form>
<input type="checkbox" name="php" value="yes" />
<input type="checkbox" name="net" value="yes" />
<input type="checkbox" name="ruby" value="yes" />
</form>
<?php
$php = $net = $ruby = 'unchecked';
if (!isset($_POST['php'],$_POST['net'],$_POST['ruby'])) {
echo 'There is no such choice';
}
else {
if (isset($_POST['php']) && $_POST['php'] == 'yes') {
$php = 'checked';
}
if (isset($_POST['net']) && $_POST['new'] == 'yes') {
$net = 'checked';
}
if (isset($_POST['ruby']) && $_POST['ruby'] == 'yes') {
$ruby = 'checked';
}
}
// ... snip ...
There are a great many ways to do this. Hopefully you will be interested in learning many of them.
Php is all server-side, so in order to keep them from submitting you'll need client-side validation. Easiest client-side validation is with javascript, or jQuery's Validation Plugin if you're already using jQuery (which you should be if you plan on using AJAX at any point).
And yes, you can get rid of those hidden inputs.
You do not need those hidden fields. Remove them and it should work.
EDIT:
Check out this modification
$ch1status = "unchecked";
$ch2status = "unchecked";
$ch3status = "unchecked";
if(isset($_POST["submit"])) {
if(#$_POST["ch1"] != "") {
$ch1status = "checked";
}
if(#$_POST["ch2"] != "") {
$ch2status = "checked";
}
if(#$_POST["ch3"] != "") {
$ch3status = "checked";
}
if (#$_POST["ch1"] . #$_POST["ch2"] . #$_POST["ch3"] == "") {
print "There is no such choice";
}
}
?>
<html>
<head>
<title>Sample form checkbxoes</title>
</head>
<body>
<form name="checkboxes" method="post" action="form_sample_checkboxes.php">
<input type="checkbox" name="ch1" value="net" <?php echo $ch1status; ?>>.NET
<input type="checkbox" name="ch2" value="php" <?php echo $ch2status; ?>>PHP
<input type="checkbox" name="ch3" value="ruby" <?php echo $ch3status; ?>>Ruby on Rails
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
if(isset($_POST["ch1"])) {
print $_POST["ch1"];
print $ch1status;
}
if(isset($_POST["ch2"])) {
print $_POST["ch2"];
print $ch2status;
}
if(isset($_POST["ch3"])) {
print $_POST["ch3"];
print $ch3status;
}
}
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
</body>
</html>

Categories