Undefined index & Invalid argument supplied for foreach() - php

I am struggling with generating an output if none of the check boxes are selected from the code below.
HTML - Form
<label>Item 1:</label><input type="checkbox" name="selected[]" value="Item 1"/>
<label>Item 2:</label><input type="checkbox" name="selected[]" value="Item 2"/>
<label>Item 3:</label><input type="checkbox" name="selected[]" value="Item 3"/>
PHP Code
<?php
foreach ($_REQUEST['selected'] as $key => $selected) {
echo "$selected";
}
?>
The code outputs the correct value when selected, but generates the "Undefined index: selected in..." & "Invalid argument supplied for foreach():"
Can anyone point me in the right direction?
Thank you

You need to check the value is set before using it:
<?php
//$_REQUEST['selected'] is set and is array
if(isset($_REQUEST['selected']) && is_array($_REQUEST['selected'])){
//Loop it
foreach ($_REQUEST['selected'] as $key=>$selected) {
echo htmlspecialchars($selected);
}
}
?>
With Undefined index: extra that could be some other error, but you think it is this. any ways if you see an Undefined index warning that means your trying to access an array key that is undefined e.g not set.
FYI: Note that $_REQUEST will accept from $_GET and $_POST, if you are not expecting that value to be set from either, it would be better tobe more specific, so if you use POST in your form then use $_POST['selected']

Related

Laravel 5.2, error on empty array

Simple situation. Multiple checkboxes:
<input name="check1[]" type="checkbox" class="magic-checkbox" value="value1_1"/>
<input name="check1[]" type="checkbox" class="magic-checkbox" value="value1_2"/>
<input name="check2[]" type="checkbox" class="magic-checkbox" value="value2_1"/>
<input name="check2[]" type="checkbox" class="magic-checkbox" value="value2_2"/>
It saves, and than i make a check if checkbox checked. Array in controller:
$checkboxes_array = array_merge($ch->check1, $ch->check2);
So, if "check2" checkboxes are not checked - i get an error:
ErrorException in adFormController.php line 70:
How to fix it?
Basically at some point you need to check if the request has the input with that name. In this case:
if(!isset(request('check1')){
$ch->check1 = [0,0];
}
if(!isset(request('check2')){
$ch->check2 = [0,0];
}
The way you checked them is prone to braking, because, in your case, $ch->check2 is not set. Try dumping dd($ch->check2) before the array_merge.

insert and edit with checkbox coding

I am a newible on php and I am following a video to learn inserting data in website to database, but I face a huge problem.
As I am going to from many groups from many members, i create some checkboxs for member to register.
I followed this video tutorial,but have error.
<?php
//$_SESSION['name'];
session_start();
echo "student";
?>
<form action="" method="POST">
<input type="checkbox" name="group[]" value="group1">group1<br>
<input type="checkbox" name="group[]" value="group2">group2<br>
<input type="checkbox" name="group[]" value="group3">group3<br>
<input type="checkbox" name="group[]" value="group4">group4<br>
<input type="checkbox" name="group[]" value="group5">group5<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit']))
{
//print_r($_POST);
echo implode(',', $POST['group']);
}
?>
</h1>
Logout
All things that I wanna do was followed for these coding but still have error
Warning: implode(): Invalid arguments passed in
C:\xampp\htdocs\fyp\student.php on line 37
echo implode(',', $POST['group']);
what was wrong with it, thank you for any suggestion
You are missing $_ before POST .
Instead of this
echo implode(',', $POST['group']);
use this
echo implode(',', $_POST['group']);
You're forgetting the underscore before the POST. Here is that line fixed:
echo implode(',', $_POST['group']);
There are a few other simple issues that might cause some trouble later on. First, in terms of syntax, you should close your HTML input tags (like below):
<input type="checkbox" name="group[]" value="group1" />group1<br>
...
Next, you will get this same error when no checkboxes are selected as the variable $_POST['group'] has not been defined yet. You can use the PHP empty() function to test this variable (http://php.net/manual/en/function.empty.php) before you call the implode().

PHP Submit Form | Input field with echo'd value in it gives undefined index

Currently I have the following form:
<form id="new_account_form" action="php/new-account.php" method="post">
<span>name</span>
</br>
<input type="text" name="main_name" required></input>
<br><br>
<span>email adres</span>
</br>
<input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>
<br><br>
<span>user</span>
</br>
<input type="text" name="main_username" required value="<?php echo $username; ?>" disabled></input>
<br><br>
</form>
As you can see both the email input field and the username input have PHP values in them that are echoed. The input field aren't empty.
The problem I am facing right now is when I submit the form and try to $_POST the input fields in the other page I keep getting the following error:
Notice: Undefined index: main_email
Notice: Undefined index: main_username
Am I echoing the variables in the wrong place or something?
The new-account.php file contains the following code:
<?php
//get data from form
$main_name = $_POST['main_name'];
$main_email = $_POST['main_email'];
$main_username = $_POST['main_username'];
echo $main_name;
echo "</br>";
echo $main_email;
echo "</br>";
echo $main_username;
echo "</br>";
?>
after a little googling, disabled forms don't post to the action page. replace disabled with readonly and you should be fine.
Replace:
<input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>
with:
<input type="text" name="main_email" required value="<?php echo $email; ?>" readonly></input>
The disabled field values of forms are not posted.So always use readonly where you want to post the value as well as want that the value remains unchanged.
Original answer:
The reason why you're getting those notices, is because the variables have not been set in the inputs' values of your form.
I was 50% right and 50% wrong.
(Consult my edit below)
Use a ternary operator
Change: value="<?php echo $email; ?>"
to
value="<?php $email=!empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"
or
value="<?php echo !empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"
and do the same for the other input.
Use a conditional !empty() for the other inputs in the other file also.
Sidenote: </input> is an invalid closing tag. </br> is also invalid, it should read as <br/>
Edit: - which is now a supplemental answer:
Upon seeing the other answer about disabled I agree on that point and they were right.
However, you will get undefined index notices in your form inputs, since those variables have not yet been set/defined.
View your HTML source (with error reporting enabled) and you will see something similar to the following:
<input type="text" name="main_email" required value="<br />
<b>Notice</b>: Undefined variable: email in <b>/path/to/your/file.php</b> on line <b>xxx</b><br />
" disabled></input>
and a similar notice for the other input.
The notices will appear in the inputs themselves.
Even though the input fields are disabled, PHP will still throw undefined notices for the variables.
Additional edit(s)
You stand at also getting Notice: Undefined variable: email in... and for the username after submission. Least, that's what my test revealed. Again; use a ternary operator for your inputs and it will solve the problem completely.
It is presently unknown as to why you're echoing the values for the inputs. If those are populated from elsewhere (a database, a file, other), either remove the variables, use a ternary operator as I already said, or use "Email" and "Username" as the default values.
Use a ternary operator as shown above.
http://php.net/manual/en/language.operators.comparison.php
Add error reporting to the top of your file(s) which will indicate errors, if any.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
if(isset($main_email))
{
echo $main_email;
}
you can use isset() function...
http://php.net/manual/en/function.isset.php

sending clicked information stored in variable from one php site to another

im having this variable:
var setValuey = function(x) {
document.getElementById('atype').value= x;
}
this refers to a clicked element:
<input type="hidden" name="type" value="" id="atype"><div class="person"><img src="images/voksenknap.png" onClick="setValuey('voksen')">
the problem is now that the set value (which happens when you click the element needs to be send to a PHP form on another site called resu.php which has this form:
form method="get" action="koeb.php">
<fieldset><legend>Billet</legend> <table border="0"><tr> <td>Type</td><td> <input type="text" name="zone" value="<?php echo (?????);?>" readonly id="atype" style="margin-left: 20px"> </td> </tr>
how do i get the set value into the value field ? whenever i am writing something in the value="<?php echo (?????);?>" all i get is a text of an undefined variable or the text setValuey:
<br /><b>Notice</b>: Undefined variable: setValuey in <b>C:\xampp\htdocs\interaktion\01-company\resu.php</b> on line <b>18</b><br />
that is what is being written in the value field for example.
hope my description is understandable. i really need help with this.
The value is put into a form input. On the server, you read that with $_POST['type'], so it should be:
value="<?php echo $_POST['type']; ?>"

Magento undefined constant

hi have a problems with magento as it shows me this error when i click sunglasses menu:
Notice: Use of undefined constant id - assumed 'id' in /var/www/app/design/frontend/glassesonline/default/template/mana/filters/items/list.phtml on line 159
This is code at line 159
<input id="category" type="hidden" value="<?php $arr = $this->getRequest()->getParams(id,false); echo $arr['id']; ?>">
This drive me crazy, please let me know where am i wrong, i will provide more info if needed.Thanks
it should be:
<input id="category" type="hidden" value="<?php $arr = $this->getRequest()->getParams('id',false); echo $arr['id']; ?>">
^^^
you are missing quotes here and PHP is thinking you are trying to use a defined varuiable which you do not have. Always use quotes for strings.

Categories