kashimpur
kashimpur

Reputation: 89

Form action page is not called after click submit button

<form class="form-inline"  action="./test.php?id=1">
                <li class="nav-item">
                    <div class="form-group">
                        <select class="custom-select custom-select-lg mb-3"  id="objectType">
                          <option selected>Object Type</option>
                          <option value="Toy">Toy</option>
                          <option value="Furniture">Furniture</option>
                          <option value="Gift">Gift</option>
                          <option value="Household">Household</option>
                          <option value="Instrument">Instrument</option>
                        </select>
                    </div>
                </li>


                <li class="nav-item">
                    <div class="form-group">
                        <select class="custom-select custom-select-lg mb-3" id="materialType">
                          <option selected>Material Type</option>
                          <option value="Mud">Mud</option>
                          <option value="Cloth">Cloth</option>
                          <option value="Thread">Thread</option>
                          <option value="Jute">Jute</option>
                          <option value="Cotton">Cotton</option>
                          <option value="Can">Can</option>
                          <option value="Bamboo">Bamboo</option>
                        </select>
                    </div>
                </li>


                <li class="nav-item">
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="search" placeholder="Search">
                            <span class="input-group-btn">
                                <button class="btn btn-outline-secondary" type="submit" id="searchSubmit"><i class="fa fa-search"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                </li>
            </form>

I have this form. When the submit button is called test.php page is called with request id value 1. In test.php form field's value will be used for making query.

test.php code snippet of a portion

$id=$_REQUEST['id'];
if ($id==1)
 {
    $result = search($_POST['imageName'],$_POST['objectType'],$_POST['materialType']);
    echo $_POST['imageName'];
}
else
{
    $result = home_page_image();
}

When I click the submit button after fill the field the test.php?id=1 is not appear in the browser address bar that confirms me that the page is not called.

Edit 1

I have added method(POST) as @PL200 mentioned in his answer but nothing is echo after search() function in test.php. I also put a alert but not shown. alert is work before search function and post value is neither echo before nor after of search function. That indicates the form values are not posted in test.php.

Edit 2
Actually I call the same page when click the submit button is clicked.

Upvotes: 0

Views: 1816

Answers (3)

kashimpur
kashimpur

Reputation: 89

There is no name of form input.

<form class="form-inline"  action="./test.php?id=1" method="POST">
                <li class="nav-item">
                    <div class="form-group">
                        <select class="custom-select custom-select-lg mb-3"  name="objectType">
                          <option selected>Object Type</option>
                          <option value="Toy">Toy</option>
                          <option value="Furniture">Furniture</option>
                          <option value="Gift">Gift</option>
                          <option value="Household">Household</option>
                          <option value="Instrument">Instrument</option>
                        </select>
                    </div>
                </li>


                <li class="nav-item">
                    <div class="form-group">
                        <select class="custom-select custom-select-lg mb-3" name="materialType">
                          <option selected>Material Type</option>
                          <option value="Mud">Mud</option>
                          <option value="Cloth">Cloth</option>
                          <option value="Thread">Thread</option>
                          <option value="Jute">Jute</option>
                          <option value="Cotton">Cotton</option>
                          <option value="Can">Can</option>
                          <option value="Bamboo">Bamboo</option>
                        </select>
                    </div>
                </li>


                <li class="nav-item">
                    <div class="form-group">
                        <div class="input-group">
                            <input class="form-control" type="search" placeholder="Search" name="imageName">
                            <span class="input-group-btn">
                                <button class="btn btn-outline-secondary" type="submit" id="searchSubmit"><i class="fa fa-search"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                </li>
            </form>

And alert message is not shown after search() method because in search() method something is wrong. I debug and correct the error in search() method. Now all works.

Upvotes: 0

John Zenith
John Zenith

Reputation: 502

I understand what you were trying to do, but note that some browsers will ignore the query arguments passed with the form action attribute. So whenever the form has been submitted, on some browsers the $_GET['id'] might not be defined, just inconsistent behavior. Also,you didn't specify any method attribute for your form.

What you should have done instead

Use a hidden input form element to pass your value, this is the recommended way to do it.

<form class="form-inline"  action="./test.php" method="post">
    <!-- put values to pass here -->
    <input type="hidden" value="1" name="id" />
  
    ...
</form>

Furthermore, you could also place the hidden input field outside the form element and use the form id to reference it with the input field form attribute. See example below:

<form id="form-id" class="form-inline"  action="./test.php" method="post">
    <!-- put values to pass here -->
    ...
</form>

<!-- Bind this hidden input field to your form -->
<input type="hidden" value="1" name="id" form="form-id" />
  

Note that there's no limitation to the numbers of hidden input field you can use in a single form, howbeit, make it simple.

Upvotes: 1

PL200
PL200

Reputation: 741

You've included no method for your form. Change the first line of your html to:

<form class="form-inline"  method="POST" action="./test.php?id=1">

That should fix the problem that you're having.

Upvotes: 2

Related Questions