Display mysql checkbox value in form - php

A simple question but I can't seem to find the answer.
I have a simple form that inserts the value 'on' to a database when a checkbox is checked
How do I get the checkbox checked when the data is pulled from the database when the form is revisited and displaying with the database information?
I have tried this but it doesn't work
<input type="checkbox" name="positioning" class="input_margin" value="<? $row['positioning']; ?>">Positioning<br />
(I only need help with this, I have the sql query etc set up fine)
Thanks

Use a conditional statement to check the database value and echo 'checked' if it should be checked
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']=='on'){echo 'checked';} ?>>

You need to use the checked property and not value.
The value field is what is provided if the box is checked (e.g positioning=on) for value="on"
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']) { ?> checked<? } ?>/>Positioning<br />

Include the value attribute so the checkbox input will post "on" if checked.
Otherwise the form will initially retrieve a value but fail to post on any subsequent form submission. to the database
<input type="checkbox" name="positioning" class="input_margin" value="on" <? if($row['positioning'] =='on'){echo 'checked';} ?> />Positioning<br />

Related

PHP Set a hook in a checkbox if the value is 1 or uncheck if the value is 0

i hope you can help me, i think the solution is simple, but i am beginner, so kick my brain.
Now i want on a new page that the checkbox will be shown as either checked or unchecked depending on the result.
The results are available as 1 or 0, but how can I now display a checkbox as checked or unchecked?
I'm grateful for every tip.
If it is Javascript you can add your PHP variable to it like so:
echo '<input type="checkbox" name="checkbox1" value="'.$checkboxValue'.">
If you wanted to get the value of the check box you have to submit it in a form like so:
<form action="index.php" method="post">
Checkbox: <input type="checkbox" name="checkbox1">
<input type="submit">
</form>
index.php
<?php
if (isset($_POST['checkbox1']){
$checkboxValue = $_POST['checkbox1'];
}
?>
Note: The $_POST value is from the name attribute of your checkbox

Checkbox and MySQL database with PHP

I've got a problem.
I don't know how to put the value of my checkbox in my MySQL database with PHP.
So :
<input type="checkbox" name="checkiard" value="IARD">
How to put the value "IARD" in my database ONLY if the checkbox is check ?
Thank you guys.
You can access the value of checkbox as any other input types. Its simply
if(isset($_POST['checkiard'])) {
$optionArray = implode(",", $_POST["checkiard"]);
}
Try this.
If you have more than one checkbox then you markup should be something like this
<input type="checkbox" name="checkiard[]" value="IARD-1">
<input type="checkbox" name="checkiard[]" value="IARD-2">
<input type="checkbox" name="checkiard[]" value="IARD-3">
you must keep the name same for all the checkbox.

How can I check if a checkbox is NOT checked

I need to check whether checkbox is checked or not. Normally I would do it like this:
<?php
$checked = isset($_POST['checkbox']);
?>
But I don't know what is the name. More at screenshot (I'm using Laravel 4).
Screenshot
You simply can't. The data for unchecked checkboxes are not send to the server.
You could do a workaround with javascript where the JS appends some hidden fields before submit with the nonchecked boxes
Supposedly you should know what the list of checkboxes is/was that you asked the user to check. Checked checkboxes are submitted to the server, unchecked ones aren't. You can calculate the difference between these two lists.
if you are using jquery, and you know the id of the checkbox,
then, you can detect with following code:
var isChecked = $("#cbId").is(":checked");
<?php
if ( ! isset($_POST['checkbox_name']))
{
"Not checked";
}
?>
If checkbox didn't check - you will not have this variable in $_REQUEST.
<form action="">
<input type="checkbox" name="ch1"/>
<input type="checkbox" name="ch2"/>
<input type="checkbox" checked="checked" name="ch3"/>
<input type="submit" name="Post" value="Post">
</form>
When you click on "Post". In backend you'll see:
<?php
if(isset($_REQUEST['ch1']))
echo 'ch1 is checked!';
if(isset($_REQUEST['ch2']))
echo 'ch2 is checked!';
if(isset($_REQUEST['ch3']))
echo 'ch3 is checked!';
?>
In my case you'll see: "ch3 is checked!".

Get several checkboxes id's after submission

I have code similar to the following:
<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...
Once the form is submitted I want to get checked checkboxes, so far I've been using
if (isset($_POST['visitProperty']) {..}
But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">
<?php
foreach($_POST['visitProperty'] as $check) {
echo $check . "<br>"; // for example
}
?>
NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]
When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.
You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).
So in case of a unique, identifyable ID, you could do something like:
<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">
The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.

How to insert multiple textbox values in databse those are checked with checkbox with php?

Below is an explanation of exactly what I want
On the submission form I have multiple checkboxs. when users click on checkbox a textbox appears next to it, where user input text. I want to insert the value of texbox in array and insert it in database with PHP-MYSQL.
for example checkbox1 is index 0=>valueoftextbox, checkbox2 is index 1=>value of texbox2
How can I achieve this?
Serialize received $_POST.
<input type="checkbox" name="smth[]" value="One" />
<input type="checkbox" name="smth[]" value="Two" />
<input type="checkbox" name="smth[]" value="Three" />
<?php
if (isset($_POST)) {
$smth = serialize($_POST['smth']);
$query = mysql_query("INSERT INTO table VALUES ('$smth')") or die(mysql_error());
}
A few pointer to get you going:
1) From PHP use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
That way you can see WHAT is posted from your client-form.
2) Note that an unchecked checkbox ISN'T posted at all.
So if you have following checkbox in your form named "wantsnewsletter".
Then check that from PHP like this:
if (isset($_POST["wantsnewsletter"])){
// It was checked.
} else {
// It wasn't checked
}
Futhermore you just have to think up some sensible naming and do the inserts into you DB.

Categories