【问题标题】:php curl populating variables with html drop down selectionphp curl 使用 html 下拉选择填充变量
【发布时间】:2018-10-08 15:25:48
【问题描述】:

我是 php 新手,并试图从 html 下拉表单中填充 php curl 请求变量。我希望此请求在选择时重新加载页面。下面的代码给出了这个错误

select.php:19 Uncaught TypeError: document.getElementById(...).submit 不是函数 变化时(select.php:19) 在 HTMLSelectElement.onchange (select.php:10)

<html>          
<head>
</head>

<body>

 <form method="post">
 <select id="wipname2" name="wipname2" style='position: relative' onchange="change()">
     <option value="site1.com">site1.com</option>
     <option value="site2.com">site2.com</option>
     <option value="site3.com">site3.com </option>
 </select>
 </form>

 <script>
 function change(){
     document.getElementById("wipname2").submit();
 }
 </script>

<?php

 $curl = curl_init();

curl_setopt_array($curl, array(
  //CURLOPT_URL => 'https://example.com/MagicService/rest/ltmNode?wipNames='.$_GET["wipnames2"],
  CURLOPT_URL => "https://example.com/MagicService/rest/ltmNode?wipNames=".'$_GET["wipnames2"]',
// CURLOPT_URL => "https://example.com/MagicService/rest/ltmNode?wipNames=content.gslb.fmr.com",
//  CURLOPT_URL => "https://example.com/MagicService/rest/ltmNode?wipNames=$value",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Cache-Control: no-cache",
    "access-token: df11c69eabb9eab4abaf4bcc6b1e62a26025e830",
    "password: ",
    "username: "
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
        //fwrite($output,$response);
//print_r (explode(" ",$response));
echo $response;

}

?>

</body>
</html>

【问题讨论】:

  • 你可以把&lt;form method="post"&gt;改成&lt;form id="theForm" method="post"&gt;再把document.getElementById("wipname2").submit();改成document.getElementById("theForm").submit();
  • 感谢@Hackerman 这工作得很好
  • @很高兴为您提供帮助!
  • 请不要在评论区发布答案。
  • 任何人都可以解释为什么 url 最后不能使用这个 '.$_GET["theForm"]

标签: javascript php ajax curl html-select


【解决方案1】:

submit() 是对表单中带有name="submit" 的文本输入的引用。

&lt;input type="hidden" name="submit" value="submit" /&gt; 放入表单中,然后它将起作用。

reference

【讨论】:

    【解决方案2】:

    问题在于这段代码document.getElementById("wipname2").submit();wipnameselect 控件,它没有 submit 事件。但是您在 form 中拥有该控件,因此您只需进行一些小的更改即可使其工作:

    1. 您可以将 &lt;form method="post"&gt; 更改为 &lt;form id="theForm" method="post"&gt; 以便在表单中添加标识符 (id)。
    2. 然后将change 函数中的document.getElementById("wipname2").submit(); 更改为document.getElementById("theForm").submit(); 以触发form 提交事件。

    【讨论】:

      猜你喜欢
      • 2017-04-17
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2018-03-12
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多