Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    $_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,

    <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?

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    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.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What version of PHP are you running on your local machine? Print out an print PHP_VERSION; to retrieve that.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi Tangoforce

    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

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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); 
    Last edited by tangoforce; 07-11-2011 at 10:28 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi Tangoforce

    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.

  • #8
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    depends on what version of PHP your localhost is running and what its settings are..try printing php_info()

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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().
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by low tech View Post
    I don't believe I admitted anything.
    No you didn't, I misread your text:
    The post was about the op not being able to get his script to work in localhost
    I read that as:
    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.
    Last edited by tangoforce; 07-11-2011 at 09:13 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi Tangoforce

    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

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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().

  • Users who have thanked Fou-Lu for this post:

    low tech (07-12-2011)

  • #13
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi Fou-Lu

    and thanks

    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

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,310
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Again, var_dump() is incredibly useful for finding out if the fields have been submitted to the script - even if they are empty.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •