Devang Sinha
Devang Sinha

Reputation: 45

PHP - Can't listen for submit button press

I am trying to build a simple HTML form that is spread over two pages. First page asks for user's first and last name. Second page asks for his country & whether he would like to receive additional info.

The first page has a single button named "Next" for going to the second page. The second page has two buttons named "Next" to go the next page (non-existent at present) and "Back" to go back to the first page.

On the first page when I click the "Next" button, nothing happens. How can I go to the next page on clicking it ?

Here is my code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
    <?php
        if(!isset($_POST['next1']))
            Display1();
        else if(isset($_POST['next1']))
            Display2();
        else if(isset($_POST['back1']))
            Display1();
    ?>
    <?php
        function Display1()
        {
    ?>
    <form id="form1" action="Multi.php" method="post">
    <label>Enter First Name: </label>
    <input type="text" name="text1"  />
    <br />
    <br />
    <label>Enter Last Name: </label>
    <input type="text" name="text2"  />
    <br />
    <br />
    <input type="button" name="next1" value="Next" />
    <?php
    }
    function Display2()
    {
    ?>
        <form id="form2" action="Multi.php" method="post">
        Select Country
        <select name="country[]" multiple="multiple">
        <option value="India">India</option>
        <option value="USA">USA</option>
        <option value="China">China</option>
        </select>
        <br />
        <br />
        Do you want to receive latest info ?
        <input type="checkbox" name="checkbox1[]"  />Yes
        <br />
        <br />
        <input type="button" name="next2" value="Next" />
        <input type="button" name="back1" value="Back" />
    <?php
    }
    ?>
</body>
</html>

Upvotes: 1

Views: 1227

Answers (2)

Max
Max

Reputation: 6791

And as an addition to Abhik's answer.

<input type="button"> is only a clickable input. You (mostly) use it when you want to call a JS script. So if for some obscure reason you want to use this instead of the more simple solution provided by Abhik go ahead and add a onclick event handler.

<input type="button" onclick="document.getElementById("form1").submit();">

You could also use a <button> instead, but make sure you always specify the type. The advantage is that they offer a higher degree of customization. Basically you can put HTML inside a <button> tag.

So try also:

<button type="submit" value="Next">Next</button>

The other types of buttons are (source: w3schools.com):

  • button: The button is a clickable button
  • submit: The button is a submit button (submits form-data)
  • reset: The button is a reset button (resets the form-data to its initial values)

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

The input type needs to be "submit"

 <input type="submit" name="next1" value="Next" />

So for all the inputs that has "button" you need to change to "submit"

Upvotes: 6

Related Questions