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 8 of 8
Like Tree1Likes
  • 1 Post By tangoforce

Thread: variable being set the second time round

  1. #1
    Regular Coder
    Join Date
    Apr 2013
    Posts
    139
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question variable being set the second time round

    hi,

    I am trying to request a querystring from a search but it only sets the second time round?!

    PHP Code:
    $searchstring = $_REQUEST['searchstring'];    


    <form action="customers-search.php?contract=<?php echo $contract;?>&searchstring=<?php echo $searchstring;?>" method="post">
    <input type="text" class="text-input small-input" id="searchstring" name="searchstring" value="<?php echo $searchstring?>"/>
    </form>
    Please help

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Your form is posting to the script called: customers-search.php

    Can you show us that script?

  • #3
    Regular Coder
    Join Date
    Apr 2013
    Posts
    139
    Thanks
    3
    Thanked 0 Times in 0 Posts
    the same code as I posted above is in that page

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    ???? What is it supposed to do?

    All you have is a form and request command.

    And your form method is "post", but you have variables in the URL. Sorry, but none of it makes any sense to me. Certainly you must have more to the script?

  • #5
    Regular Coder
    Join Date
    Apr 2013
    Posts
    139
    Thanks
    3
    Thanked 0 Times in 0 Posts
    after searching: test

    my url looks like this: http://www.domainname.co.uk/customer...&searchstring=

    if I search again, it will then look like this: http://www.domainname.co.uk/customer...rchstring=test


    Why should i have to search twice?!

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by jarv81178 View Post
    Why should i have to search twice?!
    Because you don't actually have a clue?

    Seriously I mean it.

    You're trying to set a search url with the search parameter before the parameter has even been submitted. This quite clearly shows that despite the last two years of nagging you've made virtually no effort to understand how http and html works.

    This is your form:

    PHP Code:

    <form action="customers-search.php?contract=<?php echo $contract;?>&searchstring=<?php echo $searchstring;?>" method="post">
    <input type="text" class="text-input small-input" id="searchstring" name="searchstring" value="<?php echo $searchstring?>"/>
    </form>
    The first time your page is printed, $searchstring does not exist so it contains NO VALUE. PHP however has already tried to print it (showing that you also have error reporting turned off) and so the url for your form submission is ALREADY SET to this:
    customers-search.php?contract=&searchstring=

    Why? Because you've already printed the form and the address BEFORE the user put in a search term.

    The second time around, your address is printed properly because the user has sent it - PHP then has a value and puts it into the address.

    This is a fundamental flaw of mixing php and html together - you fail to understand how the logic and code flow works. Do your php first and then insert your content into your html.

    Additionally you're trying the send the same value by POST and GET. Pick one, stick with it.

    Remember Jarv, code execution goes from top to bottom, left to right. You can't do something at the bottom and then expect to use the variable at the top of the script.

    How would I fix your form quickly?

    Set to post, and use this as the address:
    customers-search.php

    Then in your script, look for your search string in the $_POST array instead. Don't bother trying to use a url because you'll need to use the GET method instead and you *CANNOT* set the users search term in the address before they've typed it into the search box.
    Last edited by tangoforce; 04-15-2014 at 04:19 PM.
    mlseim likes this.
    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
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    What are you actually searching?

    Are you searching for a person's name in a database somewhere?

    If we knew what you were searching and what you were searching for, that would really be a good thing for us to know.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by mlseim View Post
    What are you actually searching?

    Are you searching for a person's name in a database somewhere?

    If we knew what you were searching and what you were searching for, that would really be a good thing for us to know.
    None of that will make any difference because there is no way to know what the user will be searching for to put it into the forms action url.

    Jarv simply needs to change from passing this parameter as part of a GET request into using it as part of a POST request. Thats all he can do (though I doubt he'll even understand this)
    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!


  •  

    Posting Permissions

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