Hai Truong IT
Hai Truong IT

Reputation: 4187

How to change link when submit form

I have a sample code:

$category_id = $_POST['category_id'];
<form action="search.php&category_id=$category_id" method="post">
    <p class="categories">
        <select name="category_id">
           <option value="1">Category 1</option>
           <option value="2">Category 2</option>
        </select>
    </p>
    <p class="submit">
        <input class="button" type="submit" value="Tìm game" />  
    </p> 
</form>

When I submit form is URL is search.php&category_id=0

How to fix this problem, URL is search.php&category_id=1 // OR 2

Upvotes: 0

Views: 209

Answers (2)

Max Hudson
Max Hudson

Reputation: 10206

Change method="post" to method="get" if you only want to send get data.

Keep method="post" if you want to take the user to that url and submit post data.

$("select").onchange(function(){
    var id = $(this).val();
    $("form").attr("action", $("form").attr("action") + "&category_id=" + id);
});

Upvotes: 0

deefour
deefour

Reputation: 35360

Change your <form> tag to

<form action="search.php?category_id=<?php echo $category_id?>" method="get">

The category_id in the action attribute will have it's value replaced by the value of the select.

Upvotes: 1

Related Questions