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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.