onchange选择具有Post Values的框到另一个页面

时间:2016-06-10 07:01:43

标签: php jquery ajax

我有一个带有多个选择框的表格,其中包含变量形式的不同信息,在选择任何选项框的选项时,它会被重定向到另一个页面。我正在使用此语句重定向

 <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

以上语句工作正常,但问题是变量作为查询字符串传递,我想在用户移动到其他页面时隐藏这些变量及其值。我也将表单方法更改为POST,但它不起作用。 我怎么能这样做。

4 个答案:

答案 0 :(得分:1)

使用window.location重定向始终会发送GET个请求,并且您无法以这种方式发送POST个参数。

您可以做的是让您的onchange操作提交表单。您可以使用隐藏输入发送其他参数。您可以在提交之前将<select>值附加到表单的action属性。

<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>

答案 1 :(得分:0)

只需将表单方法更改为POST即可。您需要使用 AJAX 使用POST方法将数据发送到服务器。

在此示例中使用 jQuery 可能会对您有所帮助。

您需要做的是使用.change()在选择框中绑定更改事件,然后在函数内部执行perofmr .ajax()

.ajax()的正常语法如下。

$.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.
    }
});

答案 2 :(得分:0)

您好您可以将select括在form tag中,然后使用javascript submit form action。使用form.php<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> 方式发送您的表单

// As all the entities are a kind of Node , so this interface should be implemented by all the entities . You can put some more methods in this interface, If required to be a Node type.

interface Node {
    Node getNext();
    List<? extends Node> getChildren();
}

class Root implements Node {
    private List<Partner> partners;

    @Override /*Implementation required*/
    public Root getNext() {
        return null;   // Return next node
    }

    @Override /*Implementation required*/
    public List<Partner> getChildren() {
        return partners;
    }
}

class Partner implements Node  {
    private List<Client> clients;
    @Override /*Implementation required*/
    public Partner getNext() {
        return null;  // Return next node
    }

    @Override /*Implementation required*/
    public List<Client> getChildren() {
        return clients;
    }
}

class Client implements Node {
    private List<User> users;

    @Override /*Implementation required*/
    public Client getNext() {
        return null;   // Return next node
    }

    @Override /*Implementation required*/
    public List<User> getChildren() {
        return users;
    }
}

//As per your requirement, User class is leaf node, so you can return null in getChildren() call;
class User implements Node  {
    private List<? extends Node> children;

    @Override /*Implementation required*/
    public User getNext() {
        return null;  // Return next node
    }

    @Override /*Implementation required*/
    public List<? extends Node> getChildren() {
        return children;
    }
}

答案 3 :(得分:0)

嗨试试这只是为了表达您的怀疑

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);