Let me explain more in details what I am trying to do.
Okay so, I am doing a huge form to send in a particular e-mail. My problem right now is that only my radio buttons in my form show an error when the form is submit and return false. Here's an example of what I am talking about :
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
So as you see, if the customer press on "submit" and didn't filled values inside of radio and input field, $name will show no error but $radio will show an error.
My Error :
Notice: Undefined index: number in D:\xampp\htdocs\test\preg.php on line 5
What you are getting is the server warning and not an actual error.
To avoid it you have flags that you can set to ignore warning, but this isn't a good practice.
I would suggest you to check if the form are set and handle them if not.
Code:
<?php
if (isset($_POST['submit'])) {
$radio = (isset($_POST['number']) ? $_POST['number'] : "");
$name = (isset($_POST['name']) ? $_POST['name'] : "");
}
?>
Edit: Of course, lets not forget, the better practice will be handle them against malicious code.
You get that error because $_POST['number'] is not passed to the page.
You should set at least one of the radio buttons, and if you want to be sure of not getting an error even if the users modifies the page, you can check with an isset as others as wrote in the other answers.
If you were wondering why you didn't get any error from the name, that's because it contains the empty string "".
Check this code and view the source of the page to see what print_r writes in a formatted way
<?php
if (isset($_POST['submit'])) {
$radio = $_POST['number'];
$name = $_POST['name'];
}
print_r($_POST)
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input type="radio" name="number" value="allo" checked="true">
<input type="radio" name="number" value="yo">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
You can write
if (isset($_POST['name'])) $name = $_POST['name'];
but even better is
isset($_POST['name']) ? $name = $_POST['name'] : $name = '';
if (isset($_POST['number']))
{
$radio = $_POST['number'];
}
Related
I'm learning PHP and trying to understand the if .. else statements a little better, so I'm creating a little quiz. However, I have come across an issue and I don't seem to know what the issue is. My problem is that whenever I type in the age in the input area, it will give me the $yes variable every time even if I enter the wrong age.
Here is my code so far:
My html file:
<form action="questions.php" method="post">
<p>How old is Kenny?<input></input>
<input type="submit" name="age" value="Submit"/>
</p></form>
My php file:
<?php
$age = 25;
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25){
echo "$yes";
}else{
echo "$no";
}
?>
You catch the user input inside the $_POST superglobal var (because the method of your form is POST.
So
<?php
$age = 25;
should be
<?php
$age = $_POST['age'];
There is an error in HTML too. This
<input type="submit" name="age" value="Submit"/>
should be
<input type="text" name="age" value=""/>
<input type="submit" value="Click to submit"/>
Because you want one input and one button. So one html element for each element.
and <input></input> must be cleared because it's not valid syntax :-)
<form action="questions.php" method="post">
<p>How old is Kenny?</p><input type="text" name="age"></input>
<input type="submit" value="Submit"/>
</form>
$age = (int) $_POST["age"];
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25) {
echo $yes;
} else {
echo $no;
}
<?php
/* Test that the request is made via POST and that the age has been submitted too */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['age'] ) ){
/*
ensure the age is an integer rather than a string ..
though for this not overly important
*/
$age=intval( $_POST['age'] );
if( $age==25 ) echo "Congratulations";
else echo "Bad luck!";
}
?>
<form action="questions.php" method="post">
<p>How old is Kenny?
<input type='text' name='age' placeholder='eg: 16' />
<input type="submit" value="Submit" />
</p>
</form>
A simple html form, note that the submit button does not carry the values you want to process, they are supplied via the input text element.
First of all, you need to echo the variable; echoing "$no" will keep it as a string. Remove the quotes from "$no" and "$yes" in your if then statement. Otherwise, your code seems sound!
Is there any solution for this? Sorry if I didn't explain this good.
When I put some value in $_POST['name'] and submit form I want to put that value in array( $arr ) and when I put some diferent value second time in $_POST['name'] and submit form I want that value put in array ( $arr ) to. Now that array should have two element ( first value and second value from $_POST['name'] )
<?php
session_start();
$_SESSION['name'] = 1;
$arr =array();
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$arr[] = $_POST['name'];
}
print_r($arr);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I started session, chacked if is form submitted and if is an empty field.
I tried with hidden input with value of $token = rand(); adn it not worked.
This should be easy, but I have some trouble do fix it.Tnx!
PHP is a stateless language and doesn't store variables from one page load to another. Each time you are submitting the form the $arr variable is destroyed from memory when the page has been processed.
If you want to keep information across page loads then you need to store it in the super global variable $_SESSION as a key value pair.
If you use the modified code below it will store the value typed into the name input when the form is posted in a two level multidimensional array that can be accessed using $_SESSION['name'] and a numeric index.
<?php
session_start();
if(isset($_SESSION['name']) && !is_array($_SESSION['name']))
{
$_SESSION['name'] = array();
}
if(isset($_POST['submit']) && trim($_POST['name']) != '') {
$_SESSION['name'][] = $_POST['name'];
}
print_r($_SESSION);
?>
<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
I have inside loop some fields for send inside form with other fields :
<?php
for($f=1;$f<=3;$f++)
{
?>
<input type="text" name="friend['email_<?php echo $f;?>']" value="<?php echo $_POST['friend']['email_'.$f.''];?>">
<?php
}
?>
When i send from form i need get the value of each field if no empty , i think the problem it´s in no recover the value send from the form , i don´t know if i writte right
Ony it´s that problem , thank´s , the best regards
I think the problem here is that you have single quotes around email_<?php echo $f; ?> - in POST data, these are not needed. So, you would have:
<form action="myPage.php" method="POST">
<?php for ($f=1;$f<=3;$f++) { ?>
<input type="text" name="friend[email_<?php echo $f; ?>]" value="<?php echo ... ?>">
<?php } ?>
<input type="submit">
</form>
PHP will parse POST data into associative arrays, so you will get back a 'friend' object in $_POST. You can access this like so:
<?php
$friends = isset($_POST['friend']) ? $_POST['friend'] : array();
print_r($friends);
?>
I'm not sure if i'm getting it right. You must send your form data in order to manipulate it. For example if you want to print all the form data:
Client Side:
<form action="boo.php" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit">
</form>
Server Side:
<?php
$name = "";
$age = "";
if( isset($_POST['name']) && isset($_POST['age']) )
{
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
}
echo 'Your name is ' . $name . ' and your age is ' . $age;
?>
If you are trying to retain the values even after invalid input you can just echo the input in value attribute of the input field.
Good Day!.
I need a kinda little help for PHP. I’m really really newbie for PHP and I already search it to google and can’t find any solution.
My Problem is:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
Below is my codes for PHP:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is “Notice: Undefined index: name”
<?php
$name = $_GET['name'];
echo $name;
?>
or
<?php
$name = $_POST['name'];
echo $name;
?>
Your method is post, so use $_POST
Also, try wrapping it around an isset function:
if (isset($_POST['name'])){
echo $_POST['name'];
}
This will also handle the undefined error
This would be your textbox2:
<input type="text" name="textbox2" id="name" <?php echo (isset($_POST['name']) ? 'value=' .htmlspecialchars($_POST['name']). ' : ''); ?>/>
If you want to use the GET method, change post to get in your form and $_POST to $_GET in the php code of textbox2
I'm new in PHP and I'm getting this error:
Notice: Undefined index: productid in /var/www/test/modifyform.php on
line 32
Notice: Undefined index: name in /var/www/test/modifyform.php on line
33
Notice: Undefined index: price in /var/www/test/modifyform.php on line
34
Notice: Undefined index: description in /var/www/test/modifyform.php
on line 35
I couldn't find any solution online, so maybe someone can help me.
Here is the code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">
<p>
Product ID:<br />
<input type="text" name="productid" size="8" maxlength="8" value="<?php echo $productid;?>" />
</p>
<p>
Name:<br />
<input type="text" name="name" size="25" maxlength="25" value="<?php echo $name;?>" />
</p>
<p>
Price:<br />
<input type="text" name="price" size="6" maxlength="6" value="<?php echo $price;?>" />
</p>
<p>
Description:<br />
<textarea name="description" rows="5" cols="30">
<?php echo $description;?></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</form>
<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
What I do after that (or at least I'm trying) is to update a table in MySQL.
I really can't understand why $rowID is defined while the other variables aren't.
Thank you for taking your time to answer me.
Cheers!
Try:
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['price'])) {
$price = $_POST['price'];
}
if (isset($_POST['description'])) {
$description = $_POST['description'];
}
?>
Apparently the index 'productid' is missing from your html form.
Inspect your html inputs first. eg <input type="text" name="productid" value="">
But this will handle the current error PHP is raising.
$rowID = isset($_POST['rowID']) ? $_POST['rowID'] : '';
$productid = isset($_POST['productid']) ? $_POST['productid'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';
This is happening because your PHP code is getting executed before the form gets posted.
To avoid this wrap your PHP code in following if statement and it will handle the rest no need to set if statements for each variables
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
{
//process PHP Code
}
else
{
//do nothing
}
TRY
<?php
$rowID=$productid=$name=$price=$description="";
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$productid = $_POST['productid']; //this is line 32 and so on...
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
}
There should be the problem, when you generate the <form>. I bet the variables $name, $price are NULL or empty string when you echo them into the value of the <input> field. Empty input fields are not sent by the browser, so $_POST will not have their keys.
Anyway, you can check that with isset().
Test variables with the following:
if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL
You better set it to NULL, because
NULL value represents a variable with no value.
Hey this is happening because u r trying to display value before assignnig it
U just fill in the values and submit form it will display correct output
Or u can write ur php code below form tags
It ll run without any errors
If you are using wamp server , then i recommend you to use xampp server .
you . i get this error in less than i minute but i resolved this by using (isset) function . and i get no error .
and after that i remove (isset) function and i don,t see any error.
by the way i am using xampp server
this error occurred sometime method attribute ( valid passing method )
Error option :
method="get" but called by $Fname = $_POST["name"];
or
method="post" but called by $Fname = $_GET["name"];
More info visit http://www.doordie.co.in/index.php
To remove this error, in your html form you should do the following in enctype:
<form enctype="multipart/form-data">
The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:
<form enctype="form-data/multipart">