15 replies
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?
#php
  • Profile picture of the author Leanne King
    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
    {{ DiscussionBoard.errors[246637].message }}
  • Profile picture of the author Mike Bogowski
    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
    {{ DiscussionBoard.errors[246852].message }}
  • Profile picture of the author Sean Donahoe
    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
    {{ DiscussionBoard.errors[247135].message }}
    • Profile picture of the author Lance K
      Thanks, Sean. I know virtually nothing about javascript. Isn't there a possibility that some users don't have it enabled?
      Signature
      "You can have everything in life you want if you will just help enough other people get what they want."
      ~ Zig Ziglar
      {{ DiscussionBoard.errors[247772].message }}
  • Profile picture of the author Sean Donahoe
    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
    {{ DiscussionBoard.errors[247800].message }}
    • Profile picture of the author Lance K
      Originally Posted by Sean Donahoe View Post

      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
      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?
      Signature
      "You can have everything in life you want if you will just help enough other people get what they want."
      ~ Zig Ziglar
      {{ DiscussionBoard.errors[247835].message }}
      • Profile picture of the author Neil Morgan
        In the form:

        <form method="POST" action="process.php">
        <p><select name="section">
        <option selected>Select State</option>
        <option>- - - - - - - - - - - - - - - - -</option>
        <option>California</option>
        <option>Texas</option>
        <option>New York</option>
        </select></p>
        </form>

        In process.php, something like:

        <?php

        switch($_POST['section']) {
        case('California'):
        header("Location:http://www.mydomain.com/california.html");
        break;
        case('Texas'):
        header("Location:http://www.mydomain.com/texas.html");
        break;
        }

        ?>

        This is very rough but you get the idea.

        Cheers,

        Neil
        Signature

        Easy email marketing automation without moving your lists.

        {{ DiscussionBoard.errors[247847].message }}
  • Profile picture of the author wayfarer
    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.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[247857].message }}
  • Profile picture of the author entrepenerd
    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']");
    ?>
    {{ DiscussionBoard.errors[248229].message }}
    • Profile picture of the author rwil02
      Originally Posted by entrepenerd View Post

      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>

      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
      Signature

      Roger Willcocks
      L-Space Design
      Please vote to help me win a 3kW solar array

      {{ DiscussionBoard.errors[248809].message }}
      • Profile picture of the author Lance K
        Originally Posted by rwil02 View Post

        I'd avoid doing that as you probably want to use the state for something else (like saving to a database).
        Actually, the only thing I want to use the field value for is to determine which page to send the person to after hitting submit. I don't need to save anything. I just need to get certain people to certain pages based on information (partial or whole) that they input into a field.

        For example, if I had a "favorite food" field and wanted a form submitted with the word "cream" in it to go to a page for a local creamery, would it be possible to redirect people who answer "ice cream" to the "cream" page?

        I just want to be able to send a person to a specific page upon submit based on all or part of what they input in a specific form field.

        Hope that clears it up a little.

        Thanks for all the ideas so far!
        Signature
        "You can have everything in life you want if you will just help enough other people get what they want."
        ~ Zig Ziglar
        {{ DiscussionBoard.errors[248921].message }}
        • Profile picture of the author rwil02
          Originally Posted by Lance K View Post

          Actually, the only thing I want to use the field value for is to determine which page to send the person to after hitting submit. I don't need to save anything. I just need to get certain people to certain pages based on information (partial or whole) that they input into a field.

          For example, if I had a "favorite food" field and wanted a form submitted with the word "cream" in it to go to a page for a local creamery, would it be possible to redirect people who answer "ice cream" to the "cream" page?

          I just want to be able to send a person to a specific page upon submit based on all or part of what they input in a specific form field.

          Hope that clears it up a little.

          Thanks for all the ideas so far!

          Ok. That's a bit more complicated than just matching one thing to one other thing.

          To do things like that you are looking at (at minimum) a string map a -> b, goto b
          in descending order of length (most specific match)
          eg
          ice cream -> cream
          cream -> cream
          ice -> ice cubes

          which means you need to set up and maintain a mapping file of some sort.

          beyond that you start getting into search engine type territory.
          stemming (cream = creams, creamed, creamy, etc)
          and lexographical mapping (toilet = loo, cr_pper, dunny, lavatory)

          if you just want a simple mapping, driven by a combo box, then that info can be stored in the drop down list. you can use cream as the value for both ice cream and cream display text, and that will work as long as you don't present that data back to the user again (because then it would pick the first option in the list as a match)
          Signature

          Roger Willcocks
          L-Space Design
          Please vote to help me win a 3kW solar array

          {{ DiscussionBoard.errors[249029].message }}
          • Profile picture of the author Lance K
            Actually, I'd only be interested in finding the word "cream". So whether they type "whipped cream" "ice cream" "sour cream" wouldn't matter. Anything with the word "cream" as part of the answer goes to the "cream" page. The rest of the input would be irrelevant. I just want to be able to flag certain words. Another example would be if I wanted the word "ant". If someone typed in "irrelevant" it would go to the "ant" page. Kind of like the broad search function works here. Is there a way to search a field for certain text?

            Sooner or later I'll be able to articulate exactly what I'm looking to do.

            Thanks for being patient.
            Signature
            "You can have everything in life you want if you will just help enough other people get what they want."
            ~ Zig Ziglar
            {{ DiscussionBoard.errors[249057].message }}
  • Profile picture of the author DonTino
    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
    {{ DiscussionBoard.errors[248287].message }}
  • Profile picture of the author xanol
    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...
    }
    }
    {{ DiscussionBoard.errors[249325].message }}

Trending Topics