如何在使用onchange事件提交表单后保留$ _POST?

时间:2014-10-22 23:48:41

标签: javascript php

我有一个列表视图和网格视图,它们是提交按钮,如果我点击其中一个选择选项表单将被提交,我有排序选择但我想保留$ _POST值,无论列表是否被点击或网格因为当我提交表格时数据将会消失

我想要的东西: 点击了名称,并在此之前点击了列表

enter image description here

live example:(我改变了它,但概念是相同的)

我无法删除选择下拉菜单的onchange提交,只需首先选择下拉选项,然后提交按钮,因为我需要它,以便在选择时使用新信息加载表单

我提交onchange的原因是我需要它,因为我使用此提交对项目进行排序

我的问题是我怎么能同时拥有它们?我需要两种形式吗?我需要使用jQuery吗? 或者php会做什么工作?

我在通过javascript提交后保留了选项的价值,但我不知道如何维护$ _POST的价值

如何设置$ _POST值并在onchange of sort上提交表单?

 <form method="post" action="">
    <div id="gridSort">
        <span>View Results As:</span> <span>
        <input type="submit" class="listButtons" name="view" value="List"> </span> <span>
        <input  type="submit" class="resultButtons" name="view" value="Grid"></span>
         <select id="sortSelect" class="sortSelect" size="1" name="sort" onchange="this.form.submit();">
                <option selected>Sort</option>
                <option value="Name">Name</option>
                <option value="PriceLowToHigh">Price - Low</option>
                <option value="PriceHighToLow">Price - High</option>
        </select> 
    </div>
</form>
<script type="text/javascript">
document.getElementById('sortSelect').value ="<?php if(! $_POST['sort']):?>"Sort"<?php  else:  echo $_POST['sort']; endif;?>";
</script>
<?php
if(isset($_POST["view"])):
if($_POST["view"]=="Grid")
{
    echo "Grid clicked";
    echo "<br/>";
}
if($_POST["view"]=="List" )
{
    echo "List clicked";
    echo "<br/>";
}
endif;
?>

3 个答案:

答案 0 :(得分:0)

只需在表单中添加隐藏字段:

<form method="post" action="">
    <input name="selected_sort" value="<?php echo !empty($_POST['sort']) ? strip_tags($_POST['sort']) : ''; ?>" />
    <input name="selected_view" value="<?php echo !empty($_POST['view']) ? strip_tags($_POST['view']) : ''; ?>" />
    <div id="gridSort">
        <span>View Results As:</span> <span>
        <input type="submit" class="listButtons" name="view" value="List"> </span> <span>
        <input  type="submit" class="resultButtons" name="view" value="Grid"></span>
         <select id="sortSelect" class="sortSelect" size="1" name="sort" onchange="this.form.submit();">
                <option selected>Sort</option>
                <option value="Name">Name</option>
                <option value="PriceLowToHigh">Price - Low</option>
                <option value="PriceHighToLow">Price - High</option>
        </select> 
    </div>
</form>

这将允许在所有后续表单提交中发送数据

答案 1 :(得分:0)

<form method="GET" action="">
 <body  >
<div id="gridSort">
    <span>View Results As:</span> <span>
    <input type="submit" class="listButtons" name="view" value="List"> </span> <span>
    <input  type="submit" class="resultButtons" name="view" value="Grid"></span>
     <select id="sortSelect" class="sortSelect" size="1" name="sort" onchange="this.form.submit();">
            <option selected>Sort</option>
            <option value="Name">Name</option>
            <option value="PriceLowToHigh">Price - Low</option>
            <option value="PriceHighToLow">Price - High</option>
    </select> 
</div> </body>
</form>
<script type="text/javascript">
document.getElementById('sortSelect').value ="<?php if(isset( $_GET['sort'])){ echo      $_GET['sort']; } ?>";
</script>
<?php
if(isset($_GET["view"])){
if($_GET["view"]=="Grid")
{
echo "Grid clicked";
echo "<br/>";
}
if($_GET["view"]=="List" )
{
    echo "List clicked";
   echo "<br/>";
}
}
?>

我试图重新编写你的代码工作很精细,但你需要用get替换post方法,这将有助于用户在参考和页面刷新时尽可能多。

答案 2 :(得分:0)

以下是我为像我这样会感到沮丧的人所做的事情:

<form method="post" action="" id="myform">
    <div id="gridSort">
    <input type="hidden" name="selected_sort" value="<?php echo !empty($_POST['sort']) ? strip_tags($_POST['sort']) : ''; ?>" />
    <input type="hidden" name="selectionList_view" value="<?php echo !empty($_POST['Listview']) ? strip_tags($_POST['Listview']) : ''; ?>" />
   <input  type="hidden" name="selectionGrid_view" value="<?php echo !empty($_POST['Gridview']) ? strip_tags($_POST['Gridview']) : ''; ?>" />
        <span>View Results As:</span> <span>
        <input type="submit" class="listButtons" name="Lisview" value="List"> </span> <span>
        <input  type="submit" class="resultButtons" name="Gridview" value="Grid"></span>
         <select id="sortSelect" class="sortSelect" size="1" name="sort" onchange="this.form.submit();" >
                <option selected>Sort</option>
                <option value="Name">Name</option>
                <option value="PriceLowToHigh">Price - Low</option>
                <option value="PriceHighToLow">Price - High</option>
        </select> 
    </div>
</form>
<script type="text/javascript">
document.getElementById('sortSelect').value ="<?php if(! $_POST['sort']):?>"Sort"<?php  else:  echo $_POST['sort']; endif;?>";
</script>
<pre>
<?php print_r($_POST); ?>
</pre>