Undefined index: PHP checkbox [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
The idea is to have list of checkbox for user to select genre type. The genre input is from database. Because the sql will return the result in an array, a for loop to get all the values and make them into a check box. The error come from third line in the loop. It show undefined index genre
<div class="list-group">
<h3>Genre</h3>
<?php
$search_genre = "SELECT DISTINCT(genre) FROM movie_db WHERE product_status ='1' ORDER BY movieid DESC"
$statement = mysqli_query($conn,$search_genre);
$result = mysqli_fetch_all($statement);
foreach ($result as $row){
?>
<div class="list-group-item checkbox">
<label><input type="checkbox" class="common_selector genre" value="<?php echo$row['genre'];?> > <?php echo $row['genre'];?></label>
</div>
<?php
}
?>

You need to give the checkbox a name, and since you are allowing multiple selections (checkbox vs radio button) you need to use a PHP array style name -
<label>
<input
name="mycheckbox[]"
type="checkbox"
class="common_selector genre"
value="<?php echo $row['genre'];?>"
<?php // here is where you could throw a check to make it pre-selected
by printing the word "checked" if a logical condition is met, etc ?>
>
<?php echo $row['genre'];?>
</label>
Now, in your processing script, you can reference $_POST['mycheckbox'] and it will contain an array with one or more values in it, one element per selected value.
for($i=0;$i<count($_POST['mycheckbox']);$i++){
print("selected value $i: ".$_POST[['mycheckbox'][$i]."\n");
}
Edit -
Just for giggles, if you wanted only one single selection (radio button) you STILL name the elements the same thing, you just leave off the PHP array brackets []
<label>
<input
name="myradio"
type="radio"
class="common_selector genre"
value="<?php echo $row['genre'];?>"
<?php /* here is where you could throw a
check to make it pre-selected
by printing the word "selected"
if a logical condition is met, etc
note in a series the last one selected
stays selected if you print selected more than once
*/
?>
>
<?php echo $row['genre'];?>
</label>
And the selected radio group value is available as $_POST['myradio']

Related

How to insert values in database using PHP switch statement [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
<?php
$name_user = $_POST['name'];
$city_user = $_POST['the_city'];
$conn = mysqli_connect('127.0.0.1','root','123','people');
mysqli_query($conn, "INSERT INTO people_data (name, the_city) VALUES ('$name_user','$city_user')");
echo "Data has been added:";
echo "<br/>";
echo "Name : $name_user";
echo "<br/>";
echo "The city ";
switch ($city_user)
{
case "01":
echo "London";
break;
case "02":
echo "New York";
break;
case "03":
echo "Bali";
break;
default:
echo "Unknown City";
}
?>
I have run the code, but it returns error:
Notice: Undefined index: the_city in /opt/lampp/htdocs/add.php on line 7
I see the name is added, but the city is still blank in the table people_data.
I guess the error is in switch statement, but I do not know where's the error.
Expected result in table people_data:
the_city contains value like 01 or 02 or 03
I forgot to add the html file, the html file:
<!DOCTYPE HTML>
<HTML>
<body>
<p>
Add Data
<br/>
<form action="add.php" method="post">
Name : <input type="text" name="name" /><br/>
The city :
<select>
<option value = "01">London</option>
<option value = "02">New York</option>
<option value = "03">Bali</option>
</select>
<input type="submit" value="Send">
</form>
</p>
</body>
</HTML>
I run the html file..
The problem is that your $_POST['the_city'] is NULL because you don't send it via POST. Your <select> tag has no reference name. The correct code would look like this.
<select name="the_city">
...
</select>
Furthermore, please consider using isset() method in PHP.
if (isset($_POST['the_city]))
{
//your variable has been initialized
}

PHP store html input in a global variable [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
i am building a multi step login page like that of gmail,
i want the email address the the user typed in to be stored in a global variable so i can echo it in the next step of the form i have been trying but can't figure it out, have seen many error, when i change the code i get a different one the new one i have now is (Notice: Undefined index: email in C:\xampp\htdocs\xxxxx\index.php on line 3)
this is the form
<form action="" method="post" class="form-login">
<div class="step-login step-one">
<input type="text" class="email"/>
<input type="button" class="btn next-step" value="next">
</div>
</form>
this is the php code that i am trying to use and store the input, i think this is where the problem is
<?php
$emails = $_POST['email'];
if(isset($_POST['next'])){
$GLOBALS['email'] = $_GET['email'];
}
?>
this is the code where i am trying to echo the varible
<div class="data-user-find">
<p class="user-email"><?php echo $GLOBALS['email']; ?></p>
</div>
Please guys help me
The "value" of your button is "next", but your button isn't called "next", it should be <button name="next" value="next" type="submit">
Then your error would go away. Also, you define an $email variable outside of your if-clause, please put it inside your if clause, so your code would be this:
<?php if(isset($_POST["next"]) {
$email = $_POST["email"]; (DONT FORGET TO GIVE YOUR MAIL INPUT THE 'name="mail"' TAG)
$GLOBALS["email"] = $email;
} ?>

How to I fetch Drop Down Items from Database into PHP select drop down form field [duplicate]

This question already has answers here:
mysqli_query() expects at least 2 parameters, 1 given
(5 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
Please how do i populate select dropdown list directly from Database table colunm?
Looking forward to your answers.
Thanks
<?php
include('header.php');
include('server.php');//DB Connection
?>
<div class="form-group">
<label>Select Center</label>
<?php
echo "<select class='form-control' name='center'>";
$result = mysqli_query("SELECT center_name FROM center");
while ($row = mysqli_fetch_assoc($result)) {
unset($center);
$center = $row['center'];
echo '<option value="'.$center.'"></option>';
}
echo "</select>";
?>
</div>
<div>
<button type="submit" class="btn btn-primary btn-block" name="submit" value="submit">Submit</button>
You have used column name center_name but under loop you put center as array key
Change
$center = $row['center'];
to
$center = $row['center_name'];
And you have set option values but you forgot to add your php variable inside option tag
echo '<option value="'.$center.'">'. $center .'</option>';
Thanks all. I resolved the issue by viewing the HTML sourescode through the browser and discovered an error message in the viewsource on browser which did not display to me.
The solution was that i just passed the Database connection as $con as second parameter into the mysqli_query() and that solved the problem.
Thanks all.
<div class="form-group">
<label>Select Center</label>
<select class='form-control' name='center_name'><br />
<b>Warning</b>: mysqli_query() expects at least 2 parameters, 1 given in <b>C:\xampp\htdocs\soap\create_user.php</b> on line <b>141</b><br />
<br />
<b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in <b>C:\xampp\htdocs\soap\create_user.php</b> on line <b>143</b><br />
</select>
</div>
<div>

PHP variable to URL in address bar [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have the code:
echo('
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
');
It is a form from which I select a value.
$option = isset($_POST['SelectDR']) ? $_POST['SelectDR'] : false;
if ($option){
$queryTXT = "SELECT * FROM ".$tabel." WHERE DataRaport='".$option."'";
...
The records of a SQLtable are filtered by the value selected from the form.
Now, what I want is to display in the url plan.php?DataRaport& variable php
I've tried to put in the form url (<form method="POST" action="plan.php?DataRaport'.$option.'">), but that doesn't show the variable.
You need GET not POST
<form method="GET">
First of all. If you want to echo this HTML every time, just use it as HTML:
<div class="row">
<div class="col-sm-2" >
<form method="POST" action="plan.php">
<p>Selectati data raportului:<br>
<select name="SelectDR" onchange="form.submit()">
<option value="">Select...</option>
</select>
</form>
<?php //your php stuff
To get the value from the URL, you need to change one thing:
<form method="POST" action="plan.php?DataReport=<?php echo $option?>">
Then you would get this variable as:
$dataReport = $_GET['DataReport'];
This would return the option value as POST and the DataReport as GET.
You can change the form method to GET if you wish, but you would then need to use $_GET to pick the selected option.

Undefined Index in php (I've searched all over internet, and stackoverflow, tried almost everything) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
enter code herethis is php code
$choice_spc_port = $_POST["txtSpace"]; (THIS LINE GIVES UNDEFINED INDEX NOTICE)
$choice_loc = $_POST["txtLocation"]; (THIS LINE GIVES UNDEFINED INDEX NOTICE)
And this is HTML Form
<form id="animalform" name="animalform" method="post" action="animalform.php">
<div class="wrapper"> <strong><span>*</span> Text Space portion:</strong>
<div class="formText">
<input type="radio" name="txtSpace" value="HR"/>Text space for Horse.<br />
<input type="radio" name="txtSpace" value="ZB"/>Text Space For Zebra<br />
</div>
</div>
<div class="wrapper"> <strong><span>*</span> Animal Location:</strong>
<div class="formText">
<input type="radio" name="txtLocation" value="txtSetXY"/> Specify Animal Location<br />
<div style="padding-left:20px;">
X: <input type="text" id="locField" name="txtXLocation"> <span>Taking (x=1,y=1) top left box. Increment +1 in value of X moving right</span><br />
Y: <input type="text" id="locField" name="txtYLocation"> <span>Taking (x=1,y=1) top left box. Increment +1 in value of Y moving downwards</span><br />
</div>
<input type="radio" name="txtLocation" value="Default"/>Default
</div>
</div>
</div>
Query to insert in database
$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')";
I tried the same in firefox and those two lines give notice as mentioned in bracket. Also on internet many people are looking for solution. So I think this question will help many out there.
This error shows in newer version of PHP.
There are various way to remove the error:
You can use error suppress method for this
Check that variable is not empty or set it to 0 e.g. choice_spc_port=0 at the start
Isset method also works good
Use this query.
if(isset($_POST['txtSpace'])) {
$choice_spc_port = $_POST['txtSpace'];
}
if(isset($_POST['txtLocation'])) {
$choice_loc = $_POST['txtLocation'];
}
$insert = mysql_query("INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')");
use isset for this, it will not give you undefined index notice when the $_POST['txtSpace'] and $_POST['txtLocation'] are empty and not set.
if(isset($_POST['txtSpace']))
{
$choice_spc_port = $_POST["txtSpace"];
}
if(isset($_POST['txtLocation']))
{
$choice_loc = $_POST["txtLocation"];
}
Do this to get any Post variables:
if(isset($_POST['txtSpace'])){$choice_spc_port = $_POST["txtSpace"];}
if(isset($_POST['txtLocation'])){$choice_loc = $_POST["txtLocation"];}
And to your SQL sentence:
$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ({'$choice_spc_port'}, {'$choice_loc'})";

Categories