Can PHP do this?

by 15 replies
17
Can PHP take information from a form field and send users to a specific page upon hitting the submit button depending on the value in the field?

For example, if I had a "State" field, could it take people from California to a specific page and people from New York to yet a different page?

From what little I know about PHP I would guess that an else if type deal is what I'm looking for.

If so, how would that code be put into the form so that it knows to look for the field input?

Does this make sense? I'm not sure I'm explaining it the clearest.

Thanks in advance for any help.



Edit: Is it possible to only take part of the input from the field? So if someone put Arkansas and I only wanted to take "kansas" could it take people from Arkansas to the Kansas page? In other words, is it possible to evaluate only part of the input from a given field?
#programming #php
  • I looked at doing something similar a while back and my coder was going to use a sql database and php combined. Hope that helps.

    Leanne
  • Yup!

    This is actually quite easy to do and shouldn't take anyone who knows what theyre doing more than an hour or so.

    Hope this helps
  • You can actually do this easily with JavaScript.

    Code:
    <form>
    <p><select name="section" size="1" language="javascript" onChange="pagejump(this.selectedIndex);">
    <option selected>Select State</option>
    <option>- - - - - - - - - - - - - - - - -</option>
    <option>California</option>
    <option>Texas</option>
    <option>New York</option>
    </select></p>
    </form>
    </center>
    <script language="JavaScript">
    <!--
    function pagejump(varItem)
    {
    switch(varItem)
    {
    case 0:
    window.parent.self.status="";
    break;
    case 1:
    window.parent.self.status="";
    break;
    case 2:
    window.location="http://www.yourdomain.com/california";
    break;
    case 3:
    window.location="http://www.yourdomain.com/texas";
    break;
    case 4:
    window.location="http://www.yourdomain.com/texas";
    break;
    }
    }
    // -->
    </script>
    You can put the actual javascript in an external .js file so it does not appear directly on every page and that will do the job nicely for you.

    Hope that helps!

    To Your Wealth,

    Sean Donahoe
    The Manic Marketer
    • [1] reply
    • Thanks, Sean. I know virtually nothing about javascript. Isn't there a possibility that some users don't have it enabled?
  • It's unlikely that people have javascript disabled. Though some security software may block certain JS functions. Most of the time security tools block Java Applets (not JavaScript) so you should be fine.

    To Your Wealth,

    Sean Donahoe
    The Manic Marketer
    • [1] reply
    • Thanks again, Sean. To be clear, you think it would be easier to go the JS route than PHP or PHP & MySQL? Which would be less expensive to have developed? And should cost of development be a deciding factor or are there enough other reasons to use one route over another? And finally, do you do any custom freelance programming?
      • [1] reply
  • My recommendation is not to use JavaScript, but to use the idea above, with $_POST data and header(). JavaScript is much too slow, and the header() function takes advantage of the natural HTTP header protocol, which means the browser treats the redirect as if it is being sent directly to the new page, meaning the back button will not break.
  • I'd try to avoid all the "case" statements if possible. You could easily pass the value of the page to jump as the value of the option selected like below for example:

    Code:
    <form method="POST" action="process.php">
    <p><select name="section">
    <option selected>Select State</option>
    <option>- - - - - - - - - - - - - - - - -</option>
    <option value="california.html">California</option>
    <option value="texas.html">Texas</option>
    <option value="new_york.html">New York</option>
    </select></p>
    </form>
    Then in the processor all you would need is something like:

    <?php
    header("Location: $_POST['section']");
    ?>
    • [1] reply

    • I'd avoid doing that as you probably want to use the state for something else (like saving to a database).

      I'd use the switch with a to lower case conversion to avoid potential issues.
      if ALL cases have a page to handle them, you could replace the switch with a
      PHP Code:
      head er("Loc ation: ".repl ace(_POST['section']," ""_").".html"); 
      or similar
      WARNING: Not a PHP programmer, just a programmer. and mind the random spaces added to not break the forum
      • [1] reply
  • entrepenerd gave the best and most likely most simple way to do this with PHP.

    I'd also avoid Javascript if you don't know what your doing. PHP would be the best solution for something like this.

    In the sample entrepened provided you can also use full urls like http://www.website.com/ instead of only site.html

    Tino
  • Use strpos()

    So...
    $mappings = ('cream' => 'cream url', 'ant' => 'ant url');
    $inputFromUser = strtolower($_POST['somefield']);

    foreach ($mappings as $search => $url) {
    if (strpos($inputFromUser,$search)) {
    //send to $url, or do whatever...
    }
    }

Next Topics on Trending Feed

  • 17

    Can PHP take information from a form field and send users to a specific page upon hitting the submit button depending on the value in the field? For example, if I had a "State" field, could it take people from California to a specific page and people from New York to yet a different page?