I have a form which I want to make sticky. It has lots of check boxes:
<label><span>ASD</span></label><input type="checkbox" name="condition[]" value="ASD" <?php if (in_array("ASD", $_POST['condition'])) echo 'checked'; ?> /><br />
<label><span>SLC</span></label><input type="checkbox" name="condition[]" value="SLC" <?php if (in_array("SLC", $_POST['condition'])) echo 'checked'; ?> /><br />...
Upon submit this works fine expect if the user makes no selection. I think this is something to do with the fact that the array is empty. I get an error:
Undefined index: condition
and also
in_array() expects parameter 2 to be array, null
thanks
You can check to see if the condition array was passed and if it was not make it an empty array
if(!isset($_POST['condition'])) { $_POST['condition'] = array(); }
If the user doesn't make a selection, $_POST['condition'] will not be defined. This is a simple fix by adding:
if (!isset($_POST['condition'])) $_POST['condition'] = [];
to your script.
This is because:
1) If user does not make any selection the "condition" would not be defined.
2) You are using in_array which require second parameter of type array.
But in case user does not make any selection this parameter will have undefined variable as parameter.
So you can use:
<label><span>ASD</span></label><input type="checkbox" name="condition[]" value="ASD" <?php if (!empty($_POST['condition']) && in_array("ASD", $_POST['condition'])) echo 'checked'; ?> /><br />
SLC />...
Related
I want to mark a checkbox as 'checked' automatically when an if condition is fulfilled. Here is an example of the if condition-
if($wp_session['tdwa_verification_checks'] < 2){
}
And the checkbox is-
<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1">
I am trying with this but its not working.
if($wp_session['tdwa_verification_checks'] < 2){
echo '<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1" checked>';
}
I would appreciate if anyone can give me a clue. Thanks :)
A couple issues. One is that you're wrapping you're entire checkbox inside an if statement. Another is that it's very strange to check for a boolean value by comparing it to less than 2 as you'll generally compare it to equal 1. Another is that chkboxclicked is a very vague ID to say that least, you should probably change that to something more similar to the name. You should also add the closing slash since checkbox inputs are void elements.
Now, looking at your code, you're also checking $wp_session['tdwa_verification_checks'] but the input name is tdwa-foreign-citizen, are you sure that the key you're checking in $wp_session is correct?
Lastly, WordPress has a neat function called checked() that will compare and check values for you if applicable. This is how you should probably be using it in your markup:
<input class="input-text a-save" type="checkbox" id="tdwa-foreign-citizen" name="tdwa-foreign-citizen" value="1" <?php checked( $wp_session['tdwa_verification_checks'], 1 ); ?> />
you're printing the checkbox correctly, you have to verify to value for
$wp_session['tdwa_verification_checks']
you can find the value by doing
echo json_encode($wp_session['tdwa_verification_checks']);
once you find the value you can compare correctly
How can I deal with this error:
Warning: in_array() expects parameter 2 to be array, string given in C:\xampp\htdocs\php\index.php
My code is:
if (!isset($_GET['jenis'])) {
$jenis = "";
} else {
$jenis = $_GET['jenis'];
}
<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="11" <?php if (in_array("11",$jenis)) { echo "checked"; } ?> > <a> 11 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="12" <?php if (in_array("12",$jenis)) { echo "checked"; } ?> > <a> 12 </a> </li>
<li><input type="checkbox" onclick="jeniss();" name="jenis[]" value="13" <?php if (in_array("13",$jenis)) { echo "checked"; } ?> > <a> 13 </a> </li>
Note: the error at HTML input type when the page has not posted anything yet.
You have to check $_GET['jenis'] is string or array by using
var_dump($_GET['jenis']);
if it is string than you have to define as a array just like that
$jenis = array($_GET['jenis']);
and you can use in_array
As your error says, you are trying to pass string instead of array for in_array(). Please check this to have a better look at in_array()
According to your code
if (!isset($_GET['jenis'])) {
$jenis = array(); //<--- have empty array be default.
} else {
$jenis = (array) $_GET['jenis']; //<---- change this line. You can typecast to array if you are getting only one value.
}
The second parameter needs to be an array and you are passing the string instead of array.
It is like this in_array("value_you_wanna_look",$array)
$_GET['jenis'] is a string value not an array.
You may check against the value of $jenis like this
<?php if ($jenis=="11") echo "checked"; ?> // and so on for other values
OR
Enter the value in array like this:
$jenis = array($_GET['jenis']);
In order to use in_array() you have to make sure that your second parameter is an array, you are providing a string as the second parameter.
Reason for this is that when $jenis is set by the form, it already is an array how you defined it, however before posting the form $jenis is not set and thus it will be set an empty string on line 2.
All you have to change is change line 2 to $jenis = array();.
Good practice is to make sure that a variable always has the same type or null. It will generally prevent these kind of mistakes (that might also become hard to test in complex cases).
I am retrieving some rows from a $_GET array and some of them are check box values. Since they will only display an "on" status in the $_GET array, I've devised the following solution to add an "off" to my strings that I will be sending to a client. Here is what it looks like:
if(!($_GET['active'][$row])){ //row is incremented in a loop
$oactive = "off";
}else{
$oactive = "FBINSERTa".":". "\"" . $_GET['active'][$row] ."\"";
}
Relatively simple and this works, putting off in $oactive if there is nothing at ['active'][$row]. However, if the value at $_GET['active'][$row] is undefined, PHP echoes a notice:
Notice: Undefined offset: 6 in ...\page.php
on line x
I could suppress it with error_reporting() but I'd like to find a better way. Any ideas?
Check that it's set before trying to use it, with isset
if(isset($_GET['active'][$row]) && $_GET['active'][$row]==false){
You should use isset:
if(isset($_GET['active'][$row])){ //row is incremented in a loop
$oactive = "off";
}else{
$oactive = "FBINSERTa".":". "\"" . $_GET['active'][$row] ."\"";
}
The other option is to include an input with type hidden and a default value ("off" I guess) in your markup immediately before each checkbox (and with the same name as), this way the value of the checkbox will take precidence if checked when the form is submitted, otherwise the default value from the hidden input will get submitted.
Example:
<input type="hidden" name="myCheckbox" value="off" />
<label>
<input type="checkbox" name="myCheckbox" value="on" />
Check me!
</label>
I need to insert all variables sent with post, they were checkboxes each representing a user.
If I use GET I get something like this:
?19=on&25=on&30=on
I need to insert the variables in the database.
How do I get all variables sent with POST? As an array or values separated with comas or something?
The variable $_POST is automatically populated.
Try var_dump($_POST); to see the contents.
You can access individual values like this: echo $_POST["name"];
This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”
If your post data is in another format (e.g. JSON or XML, you can do something like this:
$post = file_get_contents('php://input');
and $post will contain the raw data.
Assuming you're using the standard $_POST variable, you can test if a checkbox is checked like this:
if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
...
}
If you have an array of checkboxes (e.g.
<form action="myscript.php" method="post">
<input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
<input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
<input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
<input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
<input type="checkbox" name="myCheckbox[]" value="E" />val5
<input type="submit" name="Submit" value="Submit" />
</form>
Using [ ] in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox'] won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.
For instance, if I checked all the boxes, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:
$myboxes = $_POST['myCheckbox'];
if(empty($myboxes))
{
echo("You didn't select any boxes.");
}
else
{
$i = count($myboxes);
echo("You selected $i box(es): <br>");
for($j = 0; $j < $i; $j++)
{
echo $myboxes[$j] . "<br>";
}
}
you should be able to access them from $_POST variable:
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val<br />\n";
}
It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)
Every modern IDE will tell you:
Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)
For our solution, to get all request parameter, we have to use the method filter_input_array
To get all params from a input method use this:
$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...
Now you can use it in var_dump or your foreach-Loops
What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.
If you need to get all Input params, comming over different methods, just merge them like in the following method:
function askForPostAndGetParams(){
return array_merge (
filter_input_array(INPUT_POST),
filter_input_array(INPUT_GET)
);
}
Edit: extended Version of this method (works also when one of the request methods are not set):
function askForRequestedArguments(){
$getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
$postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
$allRequests = array_merge($getArray, $postArray);
return $allRequests;
}
So, something like the $_POST array?
You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.
Why not this, it's easy:
extract($_POST);
Using this u can get all post variable
print_r($_POST)
I am currently trying to design a for loop to iterate through any 'checked' check boxes, and from that, use the value within the 'value' slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for loop that's meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the 'Listing_ID' from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns "no checkboxes checked."
I have now hit a grey area as to why this for loop isn't working (this was taken from another example on the internet).
empty($_POST['option']) will return true, if either $_POST['option'] is not set (same as !isset($_POST['option']) (!)) or an empty array.
If you need to debug what's going on, use var_dump($_POST['option']); to find out what has been submitted for the option checkboxes. I also suggest you do a var_dump($_POST); so you can see what has been submitted overall - e.g. in case the post action is not post you will immediatly notice). For HTML output:
echo '<pre>', htmlspecialchars(print_r($_POST, true)), '</pre>';
That should give you the information you're looking for. For each individual checkbox, you can do:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
var_dump($option);
}
First of all your code seems to be bugged to me. Maybe is just a typo but
<input type="checkbox" name="option[]" value="$listing_id">
should be
<input type="checkbox" name="option[]" value="<?=$listing_id?>"/>
Moreover using empty over an array is not good at all.
Try echoing out the $option in the loop to see what the value is and there you can see if there is something there.
foreach($_POST['option'] as $option) //loop through the checkboxes
{
echo $option . "<br />";
}
Also make sure your form's method is set to POST or that it's action is pointed to the correct place. You also have an error in your input:
<input type="checkbox" name="option[]" value="$listing_id">
I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">
UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.