Jump to content

Error In Same Page Exercises: Undefined Index!


satrianixxx

Recommended Posts

hello! I'm learning to php now and I have got various problems.

 

One of them:

 

I want to maka a basic program.

 

When I type anything in a form, program will show these things at the same page. I did it and it was working but I've got error:

 

"Notice: Undefined index: buton1 in C:\xampp\htdocs\deneme\same.php on line 6"

 

 

Codes:

 

<form name="form1" action="" method="post">
<input type="textbox" name="x">
<input type="submit" name="buton1" value="submit">
</form>
<?PHP
if($_POST["buton1"]){
$data = $_POST["x"];
echo $data;
}
?>

 

How can I solve this problem?

try with single quotes

 

if (isset($_POST['buton1'])){

$data = $_POST['x'];

echo $data;

}

 

Why would it matter if single or double quotes are used?

 

But as you wrote in your code, make use of the isset function to check if there was a submitted value for the buton1 element (i.e. if the button was clicked) instead of trying to access the value directly. If the page request is not a postback, then it won't exist.

A side note, relying on a submit button value being passed with the form upon submission is unreliable. There are certain situations in certain browsers where the submit button value will not be passed when the form is submitted.

 

I recommend either using

$_SERVER['REQUEST_METHOD'] == 'POST'

or a hidden input to check whether a form has been submitted.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.