Hi everyone, here's another noobie to drive you crazy with simple questions
I am having a problem with $_POST. I am running Apache, and have a simple html forum and need it to $_post to the php script (duh). It works on an online test server I set up, but on my local machine it will not work.
[PHP]<?php
//result.php
$submit = $_POST['submit'];
if (isset($submit)){[/php
Erm .. NO!!!
NEVER rely on that for form processing. Internet Explorer has a bug with this (See my signature for more info). Relying on this technique can lead to unpredictable results and forms not being processed.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
The post was about the op not being able to get his script to work in localhost ---- I merely showed a DEMO code that worked for me using localhost --- the extra part was what I needed to add to make it work 'for me'.
If he used that 'working' code he might be able to deduct if his set up was ok or not and if not move on to identify the problem.
Although, I appreciate what you are saying and understand that you are pointing the user towards better coding practice.
LT thats the whole point though when debugging you never use code which you know to be potentially troublesome. It isn't worth it and can create several hours worth of unwanted inconvenience.
The problem with your post is that you are recommending the op to use a method which is known to be faulty to diagnose if their $_POST array has been submitted. Instead of helping to diagnose the fault that is actually opening a window for new confusion.
Quote:
Originally Posted by low tech
The post was about the op not being able to get his script to work in localhost
But that is the very problem we're trying to solve here - and you're adding in an extra potential problem and admitting it?
LT, the fact is you can not rely on a submit button. As clearly pointed out many times when testing for a form submission you should ALWAYS test for another field in the form (EG the actual content of the form NOT the button).
The best way to test this form is:
PHP Code:
if ((isset($_POST['searchtype'])) and (isset($_POST['searchterm'])))
{
//Do something here
}
However for testing the presence of the $_POST array itself:
PHP Code:
//If this outputs nothing then something IS wrong.
var_dump($_POST);
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
The post was about the op not being able to get his script to work in localhost
I read that as:
Quote:
The post was not about the op not being able to get his script to work in localhost
Apologies for that, we all misread things sometimes and I feel rather stupid for it now!
Quote:
Originally Posted by low tech
=
why does this NOT work ? It always echos searchterm even when NO data sent!
PHP Code:
if (isset($_POST['searchterm'])) { //Do something here echo "Searchterm :" . $searchterm;
}else { echo 'You have not entered search details. Please go back and try again.'; exit(); }
LT
When I run that exact code at http://67fb.freephptest.com/ it works perfectly so I suspect its something above your first line in the code box or you've got something amiss going on elsewhere.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
When I run that exact code at ... it works perfectly
Yes for me too if I run it direct in localhost using xamp --- BUT when I use it with the form and I use submit without submitting any data it always echos first echo. Any idea why?
I know how to make it work --- I just know why it doesn't work (for me) as I think it should. ie If no data submitted -- then it should echo the second echo.
This is exactly what I have in php file (in this version I moved isset to top line). I am using IE8 winxp sp3.
PHP Code:
<?php
if (isset($_POST['searchterm'])) {
$searchterm=trim($_POST['searchterm']);
//Do something here
echo "Searchterm :" . $searchterm;
}else
{
echo 'You have not entered search details. Please go back and try again.';
exit();
}
?>
Isset isn't correct. Once you submit your form, that field will be available regardless of if you have populated it or not. Checkboxes are the exception, ever other field is successful regardless of data.
What you want to use is a check against empty(), not isset().
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.