I have fetch data from database in $tableRe. Now I have to print values in textarea and also have to check the radio button.
Here is my code,
$sql = "select (address, gender) from stud table";
if($result=mysqli_query($conn,$sql)) {
while($row = mysqli_fetch_array($result)) {
$tableRe[]=$row;
}
}
<form>
Address : <br>
<textarea value="<?php echo $tableRe[0]['address']; ?>"></textarea><br>
Gender : <br>
<input type="radio" value="Male">Male
<input type="radio" value="Female">Female <br>
<input type="submit" value="Save">
</form>
Please help me regarding this. Thanks in advance.
You need to apply condition on checked HTML attribute.
Try this:
<form>
Address : <br>
<textarea><?php echo $tableRe[0]['address']; ?></textarea> <br/>
Gender : <br>
<input type="radio" value="Male" <?php echo $tableRe[0]['gender'] == 'Male' ? 'checked' : ''; ?> >Male
<input type="radio" value="Female" <?php echo $tableRe[0]['gender'] == 'Female' ? 'checked' : ''; ?>>Female <br>
<input type="submit" value="Save">
</form>
Value has to be placed between the openning and closing tags :
<texterea name="whatever">Textarea value goes here</textarea>
To set a radio/checkbox as selected/checked, you need to add to it a "checkded" attribute :
<!-- HTML4 -->
<input type="radio" name="whatever" value="value1" checked="checked" /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
<!-- HTML5 -->
<input type="radio" name="whatever" value="value1" checked /> Label 1
<input type="radio" name="whatever" value="value2" /> Label 2
<input type="radio" name="whatever" value="value3" /> Label 3
Related
I have a radio button with name and value , and I looked for many solutions but still i can't insert my radio button value on my database. My query is perfectly fine working, here is my code
<form action="" method="POST">
<label class="radio-inline"><input type="radio" name="gender" value="male">Male</label>
<label class="radio-inline"><input type="radio" name="gender" value="female">Female</label>
<input type="submit" class="btn btn-default" name="submit">
</form>
what is inserting on my database is the name not the value. so the word gender is the one who is inserting on my database.
I tired to change my gender field type into Varchar and Char but still not working.
if(isset($_POST['submit'])){
$gender = mysqli_real_escape_string($con, $_POST['gender']);
$sql = "INSERT INTO information_table (gender)
VALUES ('gender')";
Did you try like this??
<input type="radio" name="number" value="1" />
1
<input type="radio" name="number" value="2" />
2
<input type="radio" name="number" value="3" />
3
<input type="radio" name="number" value="4" />
4
<input type="radio" name="number" value="5" />
5
<?php
if (isset($_POST['submit'])){
$number = $_POST['number'];
mysql_query("INSERT INTO table_name(number) VALUES ('$number')");
}
?>
I'm trying out CodeIgniter (I'm a novice coder) and am having some trouble with POST data from a form.
I have the following code to generate a form:
<div>
<?php echo form_open('todos/update_completed/'); ?>
<?php foreach ($todos as $todo): ?>
<?php echo form_checkbox('completed', $todo->id, $todo->completed); ?>
<?php echo $todo->task; ?>
<?php echo "<br />"; ?>
<?php endforeach ?>
<br />
<?php echo form_submit('MySubmit', 'Update ToDos'); ?>
<?php echo form_close(); ?>
</div>
This generates the following code:
<form action="http://localhost:8091/index.php/todos/update_completed" method="post" accept-charset="utf-8">
<input name="completed" value="1" type="checkbox">Go to the shops
<br>
<input name="completed" value="2" checked="checked" type="checkbox">Pick up camera
<br>
<input name="completed" value="5" checked="checked" type="checkbox">Call Joey
<br>
<input name="completed" value="6" checked="checked" type="checkbox">Fill in tax return
<br>
<br>
<input name="MySubmit" value="Update ToDos" type="submit">
</form>
When I try to retrieve the POST data using:
$completed_todos = array();
$completed_todos[] = $this->input->post_get('completed');
... I allways get an array ($completed_todos) which only contains 1 (one) element - regardless how many checkboxes I checked, and it is always the latest checkbox I checked!
print_r($completed_todos); only returns the following: Array ( [0] => 6 )
Can someone please explain why I am not getting all the checkbox values returned in my array?
ps: I am following a tutorial from https://selftaughtcoders.com/creating-processing-form-codeigniter/
If there are multiple CheckBoxes with same name you should write as below
Your Check Boxes
<input name="completed" value="1" type="checkbox">
Have to change the name like this name="completed[]"
<input name="completed[]" value="1" type="checkbox">
<input name="completed[]" value="2" checked="checked" type="checkbox">
<input name="completed[]" value="5" checked="checked" type="checkbox">
<input name="completed[]" value="6" checked="checked" type="checkbox">
You need to pass the checkboxes that is selected right?
So make small modifications in your view
<input name="completed[]" value="1" type="checkbox">Go to the shops
<br>
<input name="completed[]" value="2" checked="checked" type="checkbox">Pick up camera
<br>
<input name="completed[]" value="5" checked="checked" type="checkbox">Call Joey
<br>
<input name="completed[]" value="6" checked="checked" type="checkbox">Fill in tax return
<br>
<br>
<input name="MySubmit" value="Update ToDos" type="submit">
In your controller access the checked values using following code
if(isset($_POST['MySubmit']))
{
$checkbox_value=$_POST['completed'];
var_dump($checkbox_value);
}
I have some buttons in a first_page.php, and some checkboxes in a second_page.php.
I need to select the corresponding checkbox with a query string to get this:
When "first value button" is pressed --> "second_page.php" with "my first value" checkbox already selected.
first_page.php :
<form action="second_page.php">
<input class="btn" type="submit" value="first value button">
<input class="btn" type="submit" value="second value button">
<input class="btn" type="submit" value="third value button">
</form>
second_page.php :
<form name="name" method="post" action="#">
<input type="checkbox" name="mybox[]" value="my first value"/>
<span>my first box</span><br />
<input type="checkbox" name="mybox[]" value="my second value"/>
<span>my second box</span><br />
<input type="checkbox" name="mybox[]" value="my third value"/>
<span>my third box</span><br />
</form>
You have to specify a name for each input to connect it to $_POST then in second_page.php you have to fetch the form value.
In first_page.php:
<form action="second_page.php" method="post">
<input name="first_value_btn" class="btn" type="submit" value="first value button">
<input name="second_value_btn" class="btn" type="submit" value="second value button">
<input name="third_value_btn" class="btn" type="submit" value="third value button">
</form>
In second_page.php :
<input type="checkbox" name="prodotti[]" value="my first value" <?php echo ( isset($_POST['first_value_btn']) ? 'checked="checked"' : '');?> />
Read more
I've used POST as method in my example above, you could use GET instead and then replacing $_POST with $_GET instead.
I would give the Submit buttons names and then do a check on them on the next page:
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit1'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit2'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit3'])) { echo 'checked'; ?> />
I have 3 checkboxes used for searching
<input type="checkbox" onChange="this.form.submit()" ',$var1 ? ' class="checkon" checked="checked"' : '','name="c1" value="c1">
<input type="checkbox" onChange="this.form.submit()" ',$var2 ? ' class="checkon" checked="checked"' : '',' name="c2" value="c2">';
<input type="checkbox" onChange="this.form.submit()" ',$var3 ? ' class="checkon" checked="checked"' : '',' name="c3" value="c3">';
I want them to all be on by default, but if I set them to on:
$_POST['c1'] = 'on';
Then when I uncheck the box it is still on? How do I get it on by default but off when I uncheck it?
I am not sure why you have an onChange event with these inputs. You should let the user check and uncheck whichever boxes they want and then submit all the data together. Then do if(isset($_POST['c1'])){ //Box was checked }. Also have checked="checked" just as a HTML attribute (so <input type="checkbox" name="c1" value="on" checked="checked" />) rather than doing a shorthand if statement in an onChange event.
Here would be the way I would do it:
<form action="" method="post">
<fieldset>
<input type="checkbox" name="c1" value="on" <?php if (isChecked('c1')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c2" value="on" <?php if (isChecked('c2')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c3" value="on" <?php if (isChecked('c3')) echo 'checked="checked"'; ?> />
<input type="submit" name="submit" value="Go" />
</fieldset>
</form>
<?php
function isChecked($name) { //Darkbee's edit
return empty($_POST) || isset($_POST[$name]);
}
if(isset($_POST['submit'])){
if(isset($_POST['c1'])){
//Checkbox1 was checked when the form was submitted, code goes here
}
//Do the same for c2 & c3 as necessary
}
?>
Hope this helps!!
I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value.
I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:
$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}
Thanks
It's pretty simple, take a look at the code below:
The form:
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
PHP code:
<?php
$answer = $_POST['ans'];
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
A very more efficient way to do this in php:
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
and for check boxes multiple choice:
<form action="#" method="post">
<select name="Color[]" multiple> // Initializing Name With An Array
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
// As output of $_POST['Color'] is an array we have to use foreach Loop to display individual value
foreach ($_POST['Color'] as $select)
{
echo "You have selected :" .$select; // Displaying Selected Value
}
?>
Just simply use isset($_POST['radio']) so that whenever i click any of the radio button, the one that is clicked is set to the post.
<form method="post" action="sample.php">
select sex:
<input type="radio" name="radio" value="male">
<input type="radio" name="radio" value="female">
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['radio'])){
$Sex = $_POST['radio'];
}
?>
<?php
if (isset($_POST['submit']) and ! empty($_POST['submit'])) {
if (isset($_POST['radio'])) {
$radio_input = $_POST['radio'];
echo $radio_input;
}
} else {
}
?>
<form action="radio.php" method="post">
<input type="radio" name="radio" value="v1"/>
<input type="radio" name="radio" value="v2"/>
<input type="radio" name="radio" value="v3"/>
<input type="radio" name="radio" value="v4"/>
<input type="radio" name="radio" value="v5"/>
<input type= "submit" name="submit"value="submit"/>
</form>
take a look at this code
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
php
<?php
if(isset($_POST['submit'])){
if(isset( $_POST['ans'])){
echo "This is the value you are selected".$_POST['ans'];
}
}
?>
I suggest you do it through the GET request:
for example, index.html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="button" value="submit" onclick="sendPost()" />
</form>
<script type="text/javascript">
function sendPost(){
var value = $('input[name="ans"]:checked').val();
window.location.href = "sendpost.php?ans="+value;
};
</script>
this is sendpost.php:
<?php
if(isset($_GET["ans"]) AND !empty($_GET["ans"])){
echo $_GET["ans"];
}
?>
my form:
<form method="post" action="radio.php">
select your gender:
<input type="radio" name="radioGender" value="female">
<input type="radio" name="radioGender" value="male">
<input type="submit" name="btnSubmit" value="submit">
</form>
my php:
<?php
if (isset($_POST["btnSubmit"])) {
if (isset($_POST["radioGender"])) {
$answer = $_POST['radioGender'];
if ($answer == "female") {
echo "female";
} else {
echo "male";
}
}else{
echo "please select your gender";
}
}
?>