Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2011, 03:10 AM   PM User | #1
thestudent
New to the CF scene

 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
thestudent is an unknown quantity at this point
$_POST will not work on localhost

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.

the html forum is,

Quote:
<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author
<option value="title">Title
<option value="isbn">ISBN
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text" size="40">
<br />
<input type="submit" name="submit" value="Search">
</form>
the beginning of the php script is

PHP Code:
$searchtype=$_POST['searchtype'];
  
$searchterm=trim($_POST['searchterm']);

  if (!
$searchtype || !$searchterm) {
     echo 
'You have not entered search details.  Please go back and try again.';
     exit;
  } 
Obviously I get the error message, but it works fine online, why not on localhost?
thestudent is offline   Reply With Quote
Old 07-11-2011, 03:57 AM   PM User | #2
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi

I'm still new to php myself but anyway.

Works for me on localhost.


Try this demo code.

PHP Code:
<?php
//result.php
$submit $_POST['submit'];
if (isset(
$submit)){
$searchtype=$_POST['searchtype']; 
  
$searchterm=trim($_POST['searchterm']); 

  if (!
$searchtype || !$searchterm) { 
     echo 
'You have not entered search details.  Please go back and try again.';
            exit();
  }else
    {
    echo 
"Searchtype :" $searchtype "<br />Searchterm :" $searchterm;
    }
}
?>

HTML:


Code:
<!doctype html>
<html>
<head>
<title>test demo</title>
</head>
<body>

<form action="result.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text" size="40" />
<br />
<input type="submit" name="submit" value="Search" />
</form> 

</body>
</html>
LT

Last edited by low tech; 07-11-2011 at 03:59 AM..
low tech is offline   Reply With Quote
Old 07-11-2011, 06:56 AM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,750
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
What version of PHP are you running on your local machine? Print out an print PHP_VERSION; to retrieve that.
Fou-Lu is offline   Reply With Quote
Old 07-11-2011, 09:18 AM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by low tech View Post
[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 be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-11-2011, 10:12 AM   PM User | #5
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Tangoforce

Quote:
Erm .. NO!!!
I was not suggesting that he use

$submit = $_POST['submit'];
if (isset($submit)){

in production code.

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
low tech is offline   Reply With Quote
Old 07-11-2011, 10:26 AM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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 View Post
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 be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 07-11-2011 at 10:28 AM..
tangoforce is offline   Reply With Quote
Old 07-11-2011, 11:20 AM   PM User | #7
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Tangoforce

Quote:
you're adding in an extra potential problem and admitting it
No, I didn't realise at the time of posting that I was adding a problem (my inexperience) and I don't believe I admitted anything.

Anyway, point taken.

Moving on.


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

Last edited by low tech; 07-11-2011 at 11:53 AM..
low tech is offline   Reply With Quote
Old 07-11-2011, 02:21 PM   PM User | #8
Fugix
Regular Coder

 
Join Date: Jun 2011
Posts: 103
Thanks: 0
Thanked 13 Times in 13 Posts
Fugix is an unknown quantity at this point
depends on what version of PHP your localhost is running and what its settings are..try printing php_info()
Fugix is offline   Reply With Quote
Old 07-11-2011, 09:06 PM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Fugix View Post
try printing php_info()
How will that help? It will show some system settings in a overly long html page. It won't diagnose if $_POST is working or not (and i suspect it is).

The only real way to check is to var_dump($_POST) it.

Incidentally its actually phpinfo() not php_info().
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-11-2011, 09:09 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by low tech View Post
I don't believe I admitted anything.
No you didn't, I misread your text:
Quote:
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 View Post
=
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 be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 07-11-2011 at 09:13 PM..
tangoforce is offline   Reply With Quote
Old 07-12-2011, 12:23 AM   PM User | #11
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Tangoforce

Quote:
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();
    }
?>
HTML


Code:
<!doctype html>
<html>
<head>
<title>test demo</title>
</head>
<body>
<form action="result.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="author">Author</option>
<option value="title">Title</option>
<option value="isbn">ISBN</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text" size="40" />
<br />
<input type="submit" name="submit" value="Search" />
</form> 
</body>
</html>
LT
low tech is offline   Reply With Quote
Old 07-12-2011, 01:52 AM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,750
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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().
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
low tech (07-12-2011)
Old 07-12-2011, 02:35 AM   PM User | #13
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Hi Fou-Lu

and thanks

Quote:
Isset isn't correct. Once you submit your form, that field will be available regardless of if you have populated it or not.
that explains a lot to me

I previously used isset with a form and found this problem and after research I eventually used

Code:
if (isset($_POST['somename']) && $_POST['somename'] != '')
which seemed to work.

I also saw this example from the PHP manual
PHP Code:
$isval = isset($_POST['var']) && !empty($_POST['var']); 

But although I figured there was a problem in how I was using isset when checking submitted data from a form, I never really understood it.

Your explanation has sorted that issue for me

Thanks

LT
low tech is offline   Reply With Quote
Old 07-12-2011, 01:19 PM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Again, var_dump() is incredibly useful for finding out if the fields have been submitted to the script - even if they are empty.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//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. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Tags
host, local, localhost, post

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:12 PM.


Advertisement
Log in to turn off these ads.