无法将数据从html页面发送到php

时间:2015-10-08 14:50:32

标签: php html

我正在尝试使用youtube API并尝试从html页面发送链接到我的php脚本。(我实际上正在创建一个API)。但是当我点击提交时,没有任何反应!甚至页面都没有加载

这是我的html页面

<html>
<title>Youtube Video Information</title>
<body>
<h3>Paste  Video Url in the below box</h3>
<form name = "input" name = "input" action="youtubev3.php" method="GET">
<input type = "textarea" name = "videoURL" id = "videoURL"><br>
Comments: <input type = "checkbox" name = "comments"><br>
likes: <input type = "checkbox" name = "likes"><br>
dislikes: <input type = "checkbox" name = "dislikes"><br>
Views: <input type = "checkbox" name = "dislikes"><br>
<input type="button" name="submit" value="submit" name = "submit">
</form>
</body>
</html>

这是我的php源代码。

<?php

class youtubev3{
function __construct()
{
}
function getdata(){
  $string = $_POST['videoURL'];
  $urlID = trim($string,"https://www.youtube.com/watch?v=");
  echo $urlID;
  $jsonObject = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id={$urlID}&key=AIzaSyDvM6lnjHv5YMBA6I7ROjv2yYbnohN4PNY&part=snippet,contentDetails,statistics,status");
  $json = json_decode($jsonObject);
  $comentCount = $json->{'kind'};
  //var_dump($json);
  //echo $videoType;
}
}
$getData1 = new youtubev3();
$getData1->getData();
// $videoType = $json->{'items'}[0]->{'kind'};
?>

我希望我的问题很清楚。

3 个答案:

答案 0 :(得分:1)

  1. 您的方法为GET,但您正在寻找$_POST

  2. 您的语法非常混乱。确保每个HTML标记(包括<form><input><button> - 只有一个name属性和id属性。

  3. 要发布数据,请将按钮类型更改为type="submit",而不是"button"

  4. 使用<textarea>代替<input type="textarea">,或者,对于单行输入,请使用<input type="text">

  5. 完整,更正后的代码

    <html>
        <title>Youtube Video Information</title>
        <body>
            <form action="youtubev3.php" id="input" method="post" name="input">
                <textarea id="videoURL" name="videoURL"></textarea><br>
                Comments: <input name="comments" type="checkbox"><br>
                likes: <input name="likes" type="checkbox"><br>
                dislikes: <input name="dislikes" type="checkbox"><br>
                Views: <input name="dislikes" type="checkbox"><br>
                <input name="submit" type="submit" value="submit">
            </form>
        </body>
    </html>
    

答案 1 :(得分:1)

在PHP文件中,您尝试从$_POST中获取URL,而不是从通过get方法发送数据的表单中获取。

提交按钮也有两个名称属性。

<html>
<title>Youtube Video Information</title>
<body>
<h3>Paste  Video Url in the below box</h3>
<form name = "input" name = "input" action="youtubev3.php" method="POST">
<input type = "textarea" name = "videoURL" id = "videoURL"><br>
Comments: <input type = "checkbox" name = "comments"><br>
likes: <input type = "checkbox" name = "likes"><br>
dislikes: <input type = "checkbox" name = "dislikes"><br>
Views: <input type = "checkbox" name = "dislikes"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

答案 2 :(得分:0)

检查您的HTML代码。它包含大量的间距和错误。输入类型也应该提交。