checkbox state checked php - php

How do I check another checkbox when I toggled an other checkbox?
My code looks like this:
<form method="post"><input type="checkbox" name="checkall" value = "POST" />
</form>
<?php
foreach($records as $r){
if(isset($_POST["checkall"]) == 'POST'){
$chc = "checked = 'checked'";
}
else{
$chc = "";
}
}
?>
<form method="POST">
<input type="checkbox" name="<?php echo escape($r->id); ?>" class="check" value="POST" <?php echo $chc ;?> />
</form>
The code doesn't work can anybody help me please.
Thanks a lot!
Job

...
foreach($records as $r){
if($_POST["checkall"] == 'POST')
{
...
isset() function will tell you $_POST['checkall'] is there or not and also checks it has a value in it or not.
if you want to comapare $_POST['checkall'] with some value, you have to just compare like example above. If you want both, then use two if conditions like below
...
foreach($records as $r)
{
if(isset($_POST['checkall'])
{
if($_POST["checkall"] == 'POST')
{
...
...
}
}
That would help you. First of all, before putting up the codes, plan a structure of your page, what you have to do and what visitors have to do. WIthout plan, you will end up reaching nowhere and stuck. The code you have presented is not recommended.
If you put some more codes in your questions, we could help you to some extent.

Related

Issue with php form: button input not triggering if statement

As stated above, when I try to send a form using php my if statement is not being triggered and the variable is not being set to my desired value.
Relevant PHP:
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['time_pickedm']) == "9:00"){
$timepicked = "09:00:00";
}
}
Relevant HTML:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<input type="button" class="<?php echo $buttoncolour ?>" name= 'time_pickedm' value = "9:00">
<input type="submit" class="btn btn-primary" name="Submit">
</form>
Any help would be appreciated
Your condition is wrong, try:
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST['time_pickedm']) && $_POST['time_pickedm']== "9:00"){
$timepicked = "09:00:00";
}
}
EDIT:
I think i found it!
in your html you have 2 buttons, so the time_pickedm is not going to your php. try replacing button for text:
<input type="text" class="<?php echo $buttoncolour ?>" name= 'time_pickedm' value = "9:00">
You if condition invalid .If time_pickedm is present in array is always true .
its just like if(true == "9:00")
So Change to separate validate the condition
if(isset($_POST['time_pickedm']) && $_POST['time_pickedm'] == "9:00")

Checkbox not returning checked

I have the following code:
<div><input type="checkbox" name="fhaac_publicly_queryable" value="<?php
if (isset( $_POST ['fhaac_publicly_queryable'])) {
echo "checked";
} elseif ($fhaac_publicly_queryable == "on") {
echo "checked";
}
?>" name="fhaac_publicly_queryable" /> Publicly Queryable on a search</div>
I have the content saving to the database, but on save, the checked check box disappears and I can't figure out how to query so it returns. Any ideas?
Thanks in advance
You're writing the checked attribute as the value of the value attribute. Plus, the name attribute is written twice. Do this instead:
<div>
<input type="checkbox" name="fhaac_publicly_queryable" <?php
if (isset($_POST['fhaac_publicly_queryable'])){
echo 'checked';
} else if($fhaac_publicly_queryable == "on"){
echo "checked";
}?>>
Publicly Queryable on a search
</div>
I still encourage you to please do the PHP logic somewhere else, because this just looks awful. It really does. And to edit your if/else, it could be improved with or (||).

Execute function for each element checked

I have a couple of checkboxes and I want to execute a function for each of the elements marked as checked. How can I do this?
In my particular case, I think this might be a little bit easier, because the checkboxes are constructed like this:
$get_json_values=json_decode($json_string,true);
foreach ($get_json_values as $key=>$getlikes) {
if($getlikes['type']=='like') {
?>
<div>
<input type="checkbox" name="<?php echo $getlikes['name'] ?>" value="<?php echo $getlikes['id'] ?>" checked>
<a href="https://www.facebook.com/<?php echo $getlikes['id']; ?>" target="_top">
<?php echo $getlikes['name'] ?> </a>
</div>
<?php
}}
?>
So I think it should look like this:
function doIfChecked()
{
foreach ($get_json_values as $key=>$getlikes) {
if($getlikes['type']=='like'&&<sequence that checks if checkbox is checked>)
{//do stuff}
}
}
Can anyone please help?
Thanks to Julian H. Lam, I found a good answer, but my problem is that for each of the elements , I need to do a request to the server (like a page on facebook), but I can't put any php inside javascript.
So, how could I do that since php is not allowed inside js?
Using javascript:
var checkboxes = document.querySelectorAll('input[type="checkbox"]'),
numCheckboxes = checkboxes.length,
x;
for(x=0;x<numCheckboxes;x++) {
if (checkboxes[x].checked === true) {
// add code here to be done for each checked element
// you can refer to the checked element by calling "checkboxes[x]"
}
}

PHP How to check if text field matches var

Well then, this is likely to be the n-th time someone is asking this, but honestly I didn't grab anything useful spending the last hour or so on Google. What I want to do is rather trivia, or so I thought. I have this working in Java Script but want to move it to PHP. In brief:
declare a var with a static value
add text field into which user is asked to enter value of above var
check if field is a) empty, b) non-empty mismatch, or c) non-empty match
My (limited) PHP wisdom has lead me into believing it ought to be something like the below, but apparently it's not. I'd very much appreciate any insight, tha.
<?php
$coconew = "blah";
if (isset ($_POST["cocosub"])) {
if ($_POST["cocoval"] == "") {
echo "empty";
} else {
if ($_POST["cocoval"] != $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval">
<input type="submit">
</div>
</form>
You need to change
<input type="text" id="cocoval">
to
<input type="text" name="cocoval">
There are other (and probably better) ways to do this, but you are on the right track.
$_POST only looks for the name attribute of form elements, so modify your form as such:
<?php
$coconew = "blah";
if (isset ($_POST["cocoval"])) {
if ($_POST["cocoval"] === "") {
echo "empty";
} else {
if ($_POST["cocoval"] !== $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit">
</div>
</form>
(I made a few other changes, you want to check isset on the element, not the form, it will POST to the same page if you don't give it an attribute [so no need to add the echo], and adding better type checking in your php)
in addition to the other answers already posted, you might also be interested in PHP's session support (depending on how "static" you need your static variables to be). That's where you'd put $cocoval and any other variables if you need to save their values across multiple requests for the same URL by the same user. See here for more info:
http://php.net/manual/en/features.sessions.php and
http://www.php.net/manual/en/book.session.php
This works:
<?php
session_start();
if(isset($_POST["cocosub"])){
$input = trim($_POST["cocoval"]);
if($input == ""){
echo "empty";
} elseif($input != $_SESSION["coconew"]){
echo "mismatch";
} else {
echo "match";
}
}
$_SESSION["coconew"] = substr(md5(uniqid()), 0, 5);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="cocosub" method="post">
<div>
<?php echo $_SESSION["coconew"]; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit" name="cocosub">
</div>
</form>
You needed to add name="cocosub" to the Submit button element in order for the first if(isset(...)) condition to be true. That's why the script didn't work. Also, instead of id, you need to use the name="cocoval" in the input text field as well in order for it to carry over into $_POST.

Echo value based on what checkbox is checked

yesHave a simple thing here, but not that handy in PHP. Basically I have a form that will use jquery .ajax submit based on return from PHP script. This is pseudocode for example only
HTML
<form id="makepost" name="makepost" action="PHP/wronglish_submit.php" method="post">
<input type="radio" name="logged" value="yes">Logged in
<input type="radio" name="logged" value="no">Not logged in<br>
<textarea></textarea>
<input type="submit" name="sw" id="sw" value="Submit!">
</form>
PHP
<?php
if($_POST['logged'] = "yes") {
echo "logged";
die();
} else {
echo "not_logged";
}
?>
I know that the first line is not right, can't figure out right way/most efficient way to go about this. I can handle the ajax on the return value, i just can't get it to return the right value.
thx
= is assignment.
if ($_POST['logged'] == "yes")
Since it's a sensitive thing you're doing, use a strict comparison operator for that. === and not the assignment one =.

Categories