PHP将文本框值放在url中

时间:2015-12-28 07:06:54

标签: php

当我在textbox中搜索某个字词并点击cart按钮时。我想重定向到同一个搜索页面。例如,当我搜索单词phone时,它会url这样results.php?user_query=phone&search=Search,当我点击CART按钮时,它也会有相同的url 。但在此代码中,当您搜索phone并点击CART按钮时,网址中的phone已消失results.php?user_query=&search=Search

 <form method="get" action="sample2.php" enctype="multipart/form-data">
        <input placeholder="Search" name="user_query"class="textbox"type="text"> 
        <input type="submit" name="search" value="Search" class="searchbutton">
         <input type="submit" name="add_cart1" value="cart" class="searchbutton">
 </form>

在此代码中,它没有获得user_query文本框($names = $_GET['user_query'];)的值    

if(isset($_GET['add_cart1'])){

global $con;

$ip=getIP();
$pro_id = $_GET['add_cart1'];
$check_pro = "select * from cart where ip_add='$ip' AND p_id='$pro_id'";
$run_check = mysqli_query($con, $check_pro);

    if(mysqli_num_rows($run_check)>0)
    {
        echo"<script>alert('Item is already added in the Cart!');
        window.history.back()
        </script>";
    }
    else{
     $insert_pro = "insert into cart(p_id, ip_add) value('$pro_id','$ip')";
     $run_pro = mysqli_query($con, $insert_pro);
     $names = $_GET['user_query'];
     echo"<script>

     window.open('results.php?user_query=".$names."&search=Search','_self');

     </script>";
    }
  }
 }

1 个答案:

答案 0 :(得分:-1)

您可以尝试:

<!DOCTYPE html>
<html>
<head>
    <title>Sample</title>

</head>
<body>
    <?php
        if(isset($_GET['search'])){ ?>
            <script type="text/javascript">
                window.history.replaceState("", "", "result.php?user_query=<?php echo $_GET['user_query'] ?>&search=Search");
                window.location.reload();
            </script>
<?php
        }else if(isset($_GET['add_cart1'])){
            //do something when cart button is clicked
        }
    ?>
     <form method="get" action="sample2.php" enctype="multipart/form-data">
            <input placeholder="Search" name="user_query"class="textbox"type="text"> 
            <input type="submit" name="search" value="Search" class="searchbutton">
            <input type="submit" name="add_cart1" value="cart" class="searchbutton">
     </form>
</body>
</html>