I am having a table with multiple select box carrying different information in the form of variable, While selecting any of the option of the select box it is redirected to another page. I am using this statement for redirecting
<select name="workno" onchange="javascript:window.location = 'form.php?workno=' + this.item(this.selectedIndex).value + '&counter=<?php echo $counter ?>&it=<?php echo $it?>&office=<?php echo urlencode($office) ?>';">
<option value="<?php echo $record['workno'];?>"><?php echo $record['workname'];?></option
The above statements are working fine but the problem is the variables are passed as query string, I want to hide these variable and their values when the user move to the other page. I have also changed the form method to POST, but its not working. How can i do that.
Redirecting using window.location
always sends a GET
request, and you can't send POST
parameters that way.
What you can do is make your onchange
action submit the form. You can send the additional parameters using hidden inputs. And you can append the <select>
value to the form's action
attribute before submitting it.
<form method="post" action="form.php">
<select name="workno" onchange="this.form.action += '?workno='+this.value; this.form.submit()">
<option value="<?php echo $record['workno'];?>"><?php echo $record['workname'];?></option>
... other options
</select>
<input type="hidden" name="counter" value="<?php echo $counter ?>">
<input type="hidden" name="it" value="<?php echo $it ?>">
<input type="hidden" name="office" value="<?php echo $office ?>">
</form>
Just changing the form method to POST
won't work. You need to use AJAX to send data to server using POST method.
Using jQuery in this example might help you very much.
What you need to do is bind change event on select box using .change() and then perofmr .ajax() request inside function.
Normal syntax of .ajax() is as follow.
$.ajax({
type: 'POST', //GET is also accepted.
url: YOUR_URL_TO_POST_DATA,
data:{'datakey':datavalue}, // Pass your all data here
beforeSend: function() {
// This function will trigger before sending AJAX request.
},
success: function(data)
{
// This function will trigger after successful AJAX request.
},
error: function(e,textStatus)
{
// This function will trigger if there is any error during AJAX request.
}
});
Hi You can enclose your select
in form
tag
and then use javascript
to submit
your form
. use action
to send your form in form.php
like this
<form method="post" action="form.php">
<select name="workno" onchange="this.form.submit()">
<option value="<?php echo $record['workno'];?>"><?php echo $record['workname'];?>
</select>
<input type="hidden" name="counter" value="<?php echo $counter ?>">
<input type="hidden" name="it" value="<?php echo $it ?>">
<input type="hidden" name="office" value="<?php echo $office ?>">
</form>
Hi Try this just for clear your doubt
index.php
<?php
$counter = 100;
$it = 101;
$office = 102;
?>
<table border="1px solid black">
<tr><th>ID</th><th>Name</th><th>Sub</th><th>Action</th></tr>
<tr><td>1</td><td>Mamta</td><td>PHP</td><td><form method="post" action="form.php">
<select name="workno" onchange="this.form.submit()">
<option value="">--Select---</option><option value="1">one</option><option value="2">two</option></select>
<input type="hidden" name="counter" value="<?php echo $counter ?>">
<input type="hidden" name="it" value="<?php echo $it ?>">
<input type="hidden" name="office" value="<?php echo $office ?>">
</form></td></tr>
<tr><td>2</td><td>Shipra</td><td>HTML</td><td><form method="post" action="form.php">
<select name="workno" onchange="this.form.submit()">
<option value="">--Select---</option><option value="3">three</option><option value="3">four</option></select>
<input type="hidden" name="counter" value="<?php echo $counter ?>">
<input type="hidden" name="it" value="<?php echo $it ?>">
<input type="hidden" name="office" value="<?php echo $office ?>">
</form></td></tr>
</table>
form.php
<?php
echo "<pre>";print_r($_POST);