【问题标题】:PHP: Sending Variable from Dropdown to URLPHP:将变量从下拉列表发送到 URL
【发布时间】:2014-12-19 09:03:07
【问题描述】:

关于如何通过 URL 将变量发送到其他 PHP 站点,我恳请您提供意见:

  <form action="deny.php" method="get">
  <div align="left"></div>
  <p><span class="style13" style="height: 23px;">
    <select name="deny" class="style28" style="width: 320px">
      <option value="Select" selected="selected">Select</option>
      <option value="Price">Too expensive</option>
      <option value="Agency">Other Agency</option>
    </select>
    </span></p>
  <p><span class="style13" style="height: 23px;">
  <input type="hidden" name="id" value=<? echo $id; ?> />
  </span> </p>
  <p>
  <? echo '<td><a href="deny.php?submit='.$id.'&deny='.$_GET['deny'].'">Send Feedback</a></td>'; ?>
  </p>
</form>

$id 正确,但$deny 为空

我什至尝试使用$deny (instead of $_GET['deny']) and $_POST[deny] - 但$deny 总是空的。 (可以在链接中控制)

感谢您的建议!

BR,

斯蒂芬

【问题讨论】:

  • 不能用链接提交(不用js)表单,把改成

标签: php mysql url


【解决方案1】:

用下面的代码替换你的代码:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form action="deny.php" method="get">
  <div align="left"></div>
  <p><span class="style13" style="height: 23px;">
    <select name="deny" class="style28" style="width: 320px">
      <option value="Select" selected="selected">Select</option>
      <option value="Price">Too expensive</option>
      <option value="Agency">Other Agency</option>
    </select>
    </span></p>
  <p><span class="style13" style="height: 23px;">
  <input type="hidden" name="id" value="<?php echo $id; ?>" />
  </span> </p>
  <p>
  <a href="#" id="feedbackLink">Send Feedback</a>
  </p>
</form>
<script type="text/javascript">
    $( document ).ready(function() {
        $('select[name="deny"]').change(function(){
            var link = 'deny.php?submit=<?php echo $id; ?>&deny=';
            $('a#feedbackLink').attr('href', link + $(this).val());
        });
        $('select[name="deny"]').trigger('change');
    });
</script>

不提交表单就不能使用$_GET、$_POST等变量。

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    在您将表单提交到当前脚本之前,不会填充$_GET['deny']

    不要尝试使用 PHP 来构建 URL。让浏览器来做。只需在表单中放置一个提交按钮并进行适当的输入和操作。这就是表格的用途。

    【讨论】:

      【解决方案3】:
      <form action="deny.php" method="get">
        <div align="left"></div>
        <p><span class="style13" style="height: 23px;">
          <select name="deny" class="style28" style="width: 320px">
            <option value="Select" selected="selected">Select</option>
            <option value="Price">Too expensive</option>
            <option value="Agency">Other Agency</option>
          </select>
          </span></p>
        <p><span class="style13" style="height: 23px;">
      
        </span> </p>
        <p>
        <input type="submit" value="submit">
        </p>
      </form>
      

      【讨论】:

      • 感谢您的cmets和建议。我用这个 echo "";现在一切都将通过,但按钮名称是 id 的值而不是名称“发送反馈”... :-)
      • 你想在按钮上显示哪些文字?
      【解决方案4】:
      <form action="deny.php" method="get">
          <div align="left"></div>
          <p>
              <span class="style13" style="height: 23px;">
      
                  <select name="deny" class="style28" style="width: 320px">
                      <option value="Select" selected="selected">Select</option>
                      <option value="Price">Too expensive</option>
                      <option value="Agency">Other Agency</option>
                  </select>
              </span>
          </p>
          <p>
               <span class="style13" style="height: 23px;">
                    <input type="hidden" name="id" value="<?php echo $id; ?>" /> 
               </span>
          </p>
          <p>
               <input type="submit" value="Send Feedback" value="submit" />
          </p>
      </form>
      
       <?php
            if (isset($_GET)) {
                var_dump($_GET);
            }
       ?>
      

      【讨论】:

        【解决方案5】:
        <form action="/deny.php?id=<? $id ?>" method="get">
        
        <input name="submit" type="submit" value="Feedback" />
        

        成功了 :-)

        谢谢,

        斯蒂芬

        【讨论】:

          【解决方案6】:

          试试这个,已经测试过了。

          <form action="your_page.php" method="GET"> <!--you may use POST for security as well -->
                <select name="deny">
                  <option value="">Select Option</option>
                  <option value="Price">Too expensive</option>
                  <option value="Agency">Other Agency</option>
                </select>
                <!-- you msut have $id = something -->
                <input type="hidden" name="id" value="<?php echo $id; ?>" /> 
                <input type="submit" value="Send Feedback" value="submit" />
            </form>
          
           <?php
                echo "<pre>";print_r(($_GET));
           ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-12-22
            • 2015-03-03
            • 2012-12-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多