0

First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed. here is my insert.php file :

<?php
if(isset($_POSΤ["newitem"])){

echo $itemnew = $_POSΤ["newitem"];

}


?>
<form action="insert.php" method="POST" >
    <input type="text" name="newitem">
    <input type="submit" value="Save">
</form>

EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?

NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?

6
  • make sure your file is called insert.php. Commented Sep 16, 2014 at 16:38
  • Have it echo something inside the if(){} just to make sure we're making it in to there. Either way, check for typos in names. Commented Sep 16, 2014 at 16:38
  • try remove the action="insert.php" from your form. Commented Sep 16, 2014 at 16:41
  • Add a var_dump($_POST); and see what's coming across. Commented Sep 16, 2014 at 16:41
  • 1
    did you enter value in text box :-) this code works for me . check just echo or phpinfo() Commented Sep 16, 2014 at 16:43

4 Answers 4

1

The code you have posted is perfectly valid and should work.

I'm going to guess that you do not have PHP enabled, or it is not working.

<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.

Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.

The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)

Sign up to request clarification or add additional context in comments.

1 Comment

It is uploaded on a server and i tried right clicking and i only see my html
0

Use !empty($_POST['newitem'] instead:

if(!empty($_POSΤ["newitem"])){
    echo $itemnew = $_POSΤ["newitem"];
}

empty()

Comments

0

Try the following:

if($_POST) {
  if(!empty($_POST['newitem'])) {
    $itemnew = $_POSΤ['newitem'];
    echo $itemnew; 
    // or leave it as is: echo $itemnew = $_POSΤ['newitem'];
  }
}
?>
<form action="insert.php" method="POST" >
    <input type="text" name="newitem">
    <input type="submit" value="Save">
</form>

The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.

3 Comments

@AdamSinclair yes, but why would you use 2 functions if 1 is enough. What I mean is I see a lot of if(isset() && !empty()). While !empty() is actually enough to do both
tried that as well and the same output.I think it has to do with my server
@PanagiotisIliakopoulos I would guess so. Since this code should work
0

Try this :

<?php
if(isset($_POSΤ["newitem"])){

echo $itemnew = $_POSΤ["newitem"];

}


?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
    <input type="text" name="newitem">
    <input type="submit" value="Save">
</form>

$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.