Radio buttons updating a database deletes the entry? - php

so I'm creating a small form where you can update the values of some fields pulled from a database.
Two of the fields I have however are acting wrong, when the page loads they display the information correctly but when you press 'submit' the fields in the MySQL table go blank. All the other fields update correctly including a checkbox, but not these two radio buttons.
Here's the code for it, I'm sorry to be asking this but does anyone see the error? I'm rather new with PHP so I'm not sure what to look for.
Snippet of the PHP Code (I can post the full code of it, but it's long):
<?php
if(isset($_POST['submit'])) {
$mbr=$_POST['mbr'];
$rec=$_POST['rec'];
$update = $dbconnect->query("UPDATE testing SET mbr='$mbr', rec='$rec'");
}
?>
HTML Code for the 2 radio buttons:
<input type="radio" name="mbr" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" <?php echo $noChecked; ?>>No

Nevermind I'm dumb and tired and missed the obvious solution, I forgot to put the values of the buttons in their code.
How they should be
<input type="radio" name="mbr" value="up" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" value="down" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" value="yes" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" value="no" <?php echo $noChecked; ?>>

Related

How to remember checkbox preference in user account in html/php site

Good day!
I'm doing a site. There is a checkbox which update the option I choose in phpmyadmin.
But if I press submit, then f5/reload page, the preference I choose disappears on client/html side, (look the image for better understanding)
Image, open me!
So basically I want the site remember my choice in the "user account"
I tried with
<form action="pr.php" method="POST">
<h3 style="color:red;">Numbers?</h3>
<input type="checkbox" name="a1" value="1" <?php if(isset($_POST['a1'])) echo "checked='checked'"; ?> /><label>One</label><br/>
<input type="checkbox" name="a2" value="2" <?php if(isset($_POST['a2'])) echo "checked='checked'"; ?> /><label>Two</label><br/>
<input type="checkbox" name="a3" value="3" <?php if(isset($_POST['a3'])) echo "checked='checked'"; ?> /><label>3</label><br/>
<input type="checkbox" name="a4" value="4" <?php if(!is_null($_POST['a4'])) echo "checked='checked'"; ?> /><label>Four</label><br/>
<input type="submit" value="submit" name="submit">
It doesnt work with isset, empty or is_null.
Thanks for all!
Unless you save the information on a database, even if you clicked on submit, after you refresh the webpage you are going to loose the the values of the variables.
You could use cookies to save the value of the choice that was checked:
setcookie("checkbox1","checked");
First parameter is name of the cookie, second one is the value of the cookie, and you can add a third one with the time you want the cookie to have before it expires.
And in your if condition, you could do this:
<?php if($_COOKIE['a1'] == "checked") echo "checked='checked'"; >
For more info, check this link.

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?

Radio buttons and $_POST

two problem i have:
first:
i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)
second:
why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.
and the codes:
<html>
<body>
<?php
if(isset($_POST['select sex']))
echo "it's done";
?>
<form name="input" action="" method="post">
<input type="radio" name="sex" value="male" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
"> Male<br />
<input type="radio" name="sex" value="female" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
"> Female<br />
<input type="submit" name="select sex" value="Submit" />
</form>
</body>
For checkbox, not mentioned checked attribute for any of the check box option.
For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.
if(isset($_POST['select sex']))

Keeping checkbox state after submit with status checked on first page load

I have this html form with checkboxes that keep their status checked or unchecked after submitting the form and reloading the page:
<form method="post" action="">
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_POST['keyword1'])) echo "checked='checked'"; ?> />keyword1
<input type="checkbox" name="keyword2" value="keyword2" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword2
<input type="checkbox" name="keyword2" value="keyword3" <?php if(isset($_POST['keyword2'])) echo "checked='checked'"; ?> />keyword3
<input type="submit" />
</form>
Problem is, that at first page load the checkboxes are unchecked. Is there any possibility to have all checkboxes with status checked at the beginning and then keep their new status after submit? So far I could not figure out how to do this. Any help would be much appreciated. Thanks,
you could use $_SESSION instead of $_POST :
<input type="checkbox" name="keyword1" value="keyword1" <?php if(isset($_SESSION['keyword1'])) echo "checked='checked'"; ?> />keyword1
And then put this on the top of your file :
session_start();
if (isset($_POST['my_form'])) {
if (isset($_POST['keyword1'])) {
$_SESSION['keyword1'] = 'checked';
}
}

Javascript, PHP, Saving field value

I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.

Categories