Error in Radio Button if not checked - php

Hello I am a beginner in PHP
I have created a form and there are few radio button with YES/NO options... every thing is working fine but if i submit form without clicking radio button options, it shows the following error... I know there is some code to be written but not getting exactly
'Notice: Undefined index: ws_id in D:\xampp\htdocs\mc\db_def.php on line 18'

Solution in your php use isset() to check
Use required in input tag "very basic validation"
<label for="input1">1:</label><input type="radio" name="test" id="input1" required value="1" /><br />
<label for="input2">2:</label><input type="radio" name="test" id="input2" value="2" /><br />
<label for="input3">3:</label><input type="radio" name="test" id="input3" value="3" /><br />
Check this fiddle http://jsfiddle.net/Pw5vQ/

Solution 1:
If you won't check radio button or checkbox the form won't post it so you will not find these inputs on the $_POST superglobal variable.
Try to var_dump($_POST); with checked and unchecked version.
Just check the required form name with isset() like this isset($_POST['ws_id']).
If isset gives you false it means it won't checked.
Solution 2:
You can preset the inputs with html attribute checked="checked".
For example:
You would like to check the NO radio button for default:
<input type="radio" name="ws_id" value="YES"/>Yes
<input type="radio" name="ws_id" value="NO" checked="checked"/>No

I guess you are getting this error when first accessing the page, so there is no sent data, so your $_POST['ws_id'] or $_GET['ws_id'] or $_REQUEST['ws_id'] has no value in it as there is no data sent.
Use
if (isset($_POST['id'])) // do what you were doing on line 18
//or
if (isset($_GET['id'])) // do what you were doing on line 18
//or
if (isset($_REQUEST['id'])) // do what you were doing on line 18
depending on your choice.

Related

PHP - Codeigniter radio buttons

Im using Codeigniter - I am displaying radio buttons like so :
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime"> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime"> Part Time
</fieldset>
Which works fine, they display.
Here is the code in the model.
'time'=>$this->input->post('time'),
which also works fine , it saves the choice to the database. But how do I get the radio button to populate when the page loads with the choice from the database?
You can use the form helper, which has a function called "set_radio", the very last function on this page;
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
<input type="radio" name="time" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?> />
<input type="radio" name="time" value="parttime" <?php echo set_radio('time', 'parttime'); ?> />
I hope this helps.
Just as #Craig said, using set_radio is what you will need.
php
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
Note
The value of this radio button is "2". Whatever the value is, needs to be the second parameter in set_radio.
Your code would look something like this:
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?>> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime" <?php echo set_radio('time', 'parttime'); ?>> Part Time
</fieldset>
If you want either option checked by default (when the page loads) that is where you would add the third parameter TRUE to the corresponding radio.
ALSO NOTE
In your controller, you will need to load the form_validation library for any of this to work.
php
$this->load->library('form_validation');
Hope this helps!
EDIT
My apologies, I misread the question. What I had above is...before the form is officially/successfully submitted. In your case, the form has already been (successfully) submitted and you want to...edit the form (for lack of better words) - so you need a way of populating the inputs with whatever the user chose.
What I have done in the past - in my view - is just have a ternary operation right on the element.
For example, let's say I am returning a user, and there is a "time" property.
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" value="fulltime" <? echo($user['time'] === 'fulltime') ? selected="selected" : ''; ?> />
<input type="radio" name="time" value="parttime" <? echo($user['time'] === 'parttime') ? selected="selected" : ''; ?> />
</fieldset>
Maybe that's closer to what you are looking for?

Repopulate radio buttons on failed validation (CodeIgniter)

I'm having a problem with CodeIgniter repopulating a form after validation fails.
HTML:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" <?php echo set_radio('target', 'public', TRUE); ?> />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" <?php echo set_radio('target', 'direct'); ?> />
When I first load the form I get this source code:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" checked="checked" />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" />
... so the third parameter is working (the default "TRUE")
But when I submit the form with validation errors on other fields, the form is reloaded with no radio button selected.
A var_dump($_POST) after submitting (with intentional validation errors on other fields) shows this:
array (size=9)
'target' => string 'direct' (length=6)
...
but no radio button is selected.
Got to be something simple... help?
The answer is apparently to add a fake rule to the radio button.
$this->form_validation->set_rules("tenderType", "", "trim");
Thanks to #FuzzyTree pointing to the solution here: stackoverflow.com/q/16473459/3574819

onclick set php session to keep checkbox checked. On uncheck end session

This is my form :
<input type="checkbox" name="dept" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
When i click the checkbox, the page is refreshed with the ?dept=sales in the URL. Just as i want it to be. But the checkbox is unchecked. I want the checkbox to be checked. And when the checkbox is unchecked i want the ?dept=sales to be removed from teh URL.
Thanks.
Your checkbox's name is dept, not sales. The value of your checkbox is sales. This means that if you want to access the value of your checkbox, you will need to access it via $_POST['dept'], not $_POST['sales']. If your form method isn't declared as method="post", use $_GET['dept'] instead of $_POST['dept'].
At first check your check box name, It's dept but you fetch from sales $_POST, another hint is that if your request is shown on the url then it's get not post, If you want to remove parameter from your url add method="post" to your form, At last your code should be like this:
<form action="your action here" method="post">
<input type="checkbox" name="sales" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
</form>
You're checking for an input with the name "sales" change $_POST['sales'] to $_POST['dept'] that'll work :)
Submit the form only when the checked property true like,
<input type="checkbox" name="dept" value="sales"
<?php if($_REQUEST['dept']=='sales')) echo "checked='checked'"; ?>
onclick="if(this.checked==true) this.form.submit();" /><br />

How to mantain radiobutton, dropdown and checkbox values if submit fails PHP

I know how to it with text inputs. I can easily put a php script in its value, but doing it with input groups seems different. How can I mantain the values of group inputs if the submission of the form fails?
To re-mark a checkbox or radio button as checked, you use this code:
<input type="checkbox" name="foo" id="foo" checked="checked"/>
The key is checked="checked".
If you are using groups of checkboxes, make sure the name of the field ends with brackets [], like this:
<input type="checkbox" name="foo[]" id="foo_1" value="1" checked="checked"/>
<input type="checkbox" name="foo[]" id="foo_2" value="2" checked="checked"/>
Then your $_REQUEST['foo'] variable will automatically be an array of checked values. You can use in_array to see if a particular checkbox was checked.
Update based on comment
Here's how I would set it:
<input type="checkbox" name="foo[]" id="foo_1" value="1" <?= (isset($_POST['foo'] && in_array('1', $_POST['foo'])) ? 'check="checked"' : '' ?>/>
For single items (like radios), use this:
<input type="radio" name="foo" id="foo" value="1" <?= isset($_POST['radio]) ? 'check="checked"' : '' ?>/>
Hope that helps.
Update 2:
Also, make sure you escape user input! Your example should look like this:
<input type="text" name="username" value="<?php if(isset($_POST['username']) echo htmlspecialchars($_POST['username']);?>">
Always assume the user is trying to hack your system, always escape user input!
Print the " checked" attribute for radio buttons and checkbox input tags, or the " selected" attribute for dropdown option tags.

PHP post checkbox problem

i have a form that utilizes checkboxes.
<input type="checkbox" name="check[]" value="notsure"> Not Sure, Please help me determine <br />
<input type="checkbox" name="check[]" value="keyboard"> Keyboard <br />
<input type="checkbox" name="check[]" value="touchscreen"> Touch Screen Monitors <br />
<input type="checkbox" name="check[]" value="scales">Scales <br />
<input type="checkbox" name="check[]" value="wireless">Wireless Devices <br />
And here is the code that process this form in a external php file.
$addequip = implode(', ', $_POST['check']);
I keep getting this error below;
<b>Warning</b>: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>.../process.php</b> on line <b>53</b><br />
OK
is any of your checkboxes ticked? php’s $_POST array will only have checkboxes which have been ticked
to silence your warning use this:
$addequip = implode(', ', empty($_POST['check']) ? array() : $_POST['check'] );
the following site seems to be what you need:
http://www.ozzu.com/programming-forum/desperate-gettin-checkbox-values-post-php-t28756.html
Hi I m the original user who posted this question i couldnt login to my account so posting from another account. After couple hours of trying i somehow managed to make it work partially. Below is the modified form html and process code for checkboxes
<input type="checkbox" name="check" value="Touchscreen"> Touchscreen<br>
<input type="checkbox" name="check" value="Keyboard"> Keyboard<br>
<input type="checkbox" name="check" value="Scales"> Scales<br>
I had to remove the [] so it would work. Also below is the entire post method for those would like to see. It works perfectly fine with every other field.
<form id="contact_form" action="process.php" method="POST" onSubmit="return processForm()">
And below is the php code to process checkboxes. For some reason i have to tell script that $_POST['check'] is an array without it would only return array. All other methods suggested returns invalid argument passed error.
$chckbox = array($_POST['check']);
if(is_array($chckbox))
{
foreach($chckbox as $addequip) {
$chckbox .="$addequip\n";
}
}
So this code works but returns only 1 checkbox value that is ticked even no matter how many you ticked.

Categories