3

I have already asked a question regarding this issue here, for a client project. Those answers may not be what I am looking for, so I will try to make this question as detailed as possible.

I have a form which has 4 main pages. The first page asks for personal information. Clicking on Submit takes you to a second page. Second page has a dropdown list containing item numbers. Let's say they are A, B, C, D, E.

When a visitor chooses an item and clicks submit, I want the next page to show content based on the item chosen above:

  • If visitor chooses option A, the page should load the html page a.html or php page a.php.
  • If he selects option B, the page should load the html page b.html or php page b.php. And so on for other items, each one having a different page.

I don't know PHP in detail. I am building this form in a website on WordPress.

Community
  • 1
  • 1
akumarks
  • 71
  • 1
  • 1
  • 7
  • how many fields depend on the 1st page and so on? jquery would be a much more elegant solution but if you insist on php that can be done also. You will need to update your question with form fields and what needs to change on the next page, easy enough to do with both solutions and easy to learn! – David Jul 06 '14 at 21:55
  • SO is not a free "write my code"-service. If you don't want to invest enough time learning this, then pay someone. (I really don't get, how such a question gets three upvotes...) – Yoshi Jul 07 '14 at 11:37
  • @Yoshi I know for a fact that Stack Overflow is a website where people come to share knowledge, ask questions and give answers. If someone needed help and if I knew about it, I would be more than happy to write them a code. The answers on this question clearly show that not everybody here thinks Stack Overflow is a "write my code"-service, but a community where people contribute. Btw, I just started learning PHP and will surely be back later to help answer questions from others and YES, I will be glad to write them a code... :) Have a good day!!! – akumarks Jul 07 '14 at 19:35

3 Answers3

4

You can get around this problem in many ways, but in this case I would recommend two ways: PHP or JS.

Solution 1, PHP

You will need

1) A form

<form action="loadPage.php" method="POST" name="theForm" id="theForm">
<select form="theForm" name="selectedPage">
  <option value="page_1">Page 1</option>
  <option value="page_2">Page 2</option>
  ...
</select>
<input type="submit" value="Load page" />
</form>

2) A PHP handler

//loadPage.php
<?php
$requested_page = $_POST['selectedPage'];

switch($requested_page) {
  case "page_1":
    header("Location: page_1.php");
  break;
  case "page_2":
    header("Location: page_2.php");
  break;
  default :
  echo "No page was selected";
  break;
}
?>

Solution 2, Javascript

Let's say that you'd like to display different pages without using PHP, then what to choose? JavaScript. This is, to me, more easy to implement and much more "user-friendly" :

Task: Display different pages from the same form

<!DOCTYPE html>
<html>
<head>
<!-- Amazing stuff goes here :) -->
<script>
function load_page() {
    var selected_page = document.getElementById("selected_page").value;
    if (selected_page != "") {
        window.location.href = selected_page
        //Please note that the value recived,
        //in this case selected_page,
        //should be a valid url!
        //Therefore the value of the
        //<option> tag should be itself 
        //a url !
        //ex.: <option value="page.php"> is valid
        //<option value="page_1"> is not valid
    }
}
</script>
</head>
<body>
<form action="#">
    <select id="selected_page">
        <option value="page_1.php">Page 1</option>
        <option value="page_2.php">Page 2</option>
        ...
    </select>
    <button onclick="load_page()">Load it ! </button>
</form>
</body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186
Alberto
  • 4,212
  • 5
  • 22
  • 36
  • I appreciate the reply. But, I am not sure what to do here. I never intended to build PHP forms, but am forced to. With your code, I created a page template and named it formorder.php. In it, I have placed both your code snippets. There wasn't a submit button so I added it in the place of ... you have above. Now, on loading the form, I see Page 1 and Page 2 in dropdown list, but it says "No page was selected" and on clicking on Submit, I get Object not found! Forgive my ignorance, but I am no expert in PHP or WordPress. Would appreciate a step-by-step guide. Thanks for taking your time... – akumarks Jul 06 '14 at 20:21
  • Ok, then are you 100% sure that the and case "A" should be the same. Try that way. I'll update the code adding the form and try myself the code to help you. In 10 minutes you will be ok. – Alberto Jul 06 '14 at 20:32
  • Ok, the code works fine as expected. Make sure that: Method is POST on the form and POST onto the php handler. Or even GET and GET. Double check the values of the – Alberto Jul 06 '14 at 20:38
  • Nope, didn't work. Since you got it working, I may not be doing this right. So, here's what I have done in my WordPress theme directory. -- Created page template "formorder.php" and created a page with it. -- Added both your code snippets into the page. -- Created page_1.php and page_2.php in same directory. -- The form loads and on clicking Load page submit button, the page tries to load http://localhost/wordpress/loadPage.php since I am working on my computer. It says object not found. My question is why is the loadPage.php needed when it isn't created anywhere or loaded anywhere? – akumarks Jul 06 '14 at 21:05
  • replace the form action with "#" – David Jul 06 '14 at 21:56
  • Well let me understand, you placed php and html in the same file? My bad, you need to make 4 pages for this tutorial: 1 page for the form, the other for the php handler, and page1 & page2 – Alberto Jul 06 '14 at 22:51
  • Probably I am not getting what you are doing wrong, any image and/or complete code? – Alberto Jul 06 '14 at 22:52
  • Finally, we can state that this is not a programming issue and therefore try to double check folders and dirs :) – Alberto Jul 06 '14 at 22:56
  • @FoxNos Your solution is maybe the best one, but I am yet to figure out what goes where. I still couldn't get this to work on my WordPress. So, I am going to try it with just php files on my xampp and see if it works. Thanks for everything.... Have a good one! – akumarks Jul 07 '14 at 19:19
  • For anything else, I'm at your disposition. Best regards. – Alberto Jul 07 '14 at 22:45
  • @akumarks: the PHP solution as presented by FoxNos should work - you just need to work out what the `action` will be, since you are working inside WordPress. Maybe try removing it entirely, so it posts to self. – halfer Jul 08 '14 at 03:55
0

It is usually better to dynamically change the content via echo-ing different html, but this can be achieved through headers.

Just be sure not to output ANYTHING before the header calls or it will not work properly. Running PHP commands etc are fine as long as no html is sent to the client before you do your redirect.

if($_POST['DropDownName'] = 'ValueA')
    header("Location: a.php");
else // if(POST....) should check just in case.
    header("Location: b.php");

Another possible solution is to use an iframe to load the next page, one benefit of this is that you can do it all in JS

Cheruvian
  • 5,628
  • 1
  • 24
  • 34
-1

You can use an AJAX function to achieve the above mentioned result. Now we are in second page which has a drop down menu.

<select name="selectpageddl" class="form-control" onchange="selectpage(this.value)">
<option value="page_a" selected="selected">A</option>
<option value="page_b" selected="selected">B</option>
<option value="page_c" selected="selected">C</option>
<option value="page_d" selected="selected">D</option>
<option value="page_e" selected="selected">E</option>
</select>



 <div id="showpage"></div>

And in the onchange property of the dropdown, I'm calling an AJAX function selectpage(), to which I pass the currently selected value from drop down...SO based on that value, appropriate page will be loaded & shown inside the showpage div.

function selectpage(ipp)
{

    if(window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();

    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()//callback fn
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            document.getElementById("showpage").innerHTML=xmlhttp.responseText;


        }
    }

    if(ipp=='page_a')
    {
    xmlhttp.open("GET","a.php?variable="+ipp,true);
    }
    else if(ipp=='page_b')
    {
    xmlhttp.open("GET","b.php?variable="+ipp,true);
    }
   else if(ipp=='page_c')
    {
    xmlhttp.open("GET","c.php?variable="+ipp,true);
    }
   else if(ipp=='page_d')
    {
    xmlhttp.open("GET","d.php?variable="+ipp,true);
    }
    else 
    {
    xmlhttp.open("GET","e.php?variable="+ipp,true);
    }
    // alert(xmlhttp);
    xmlhttp.send();

}

And in those corresponding, pages.....echo whatever you want to echo.

halfer
  • 19,824
  • 17
  • 99
  • 186
Smokey
  • 1,857
  • 6
  • 33
  • 63