0

I am trying to work with youtube API and trying to send a link from html page to my php script.(I am actually creating an API). But when I click on submit, nothing happens !! even the page is not loaded

Here is my html page

<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>

And here is my php source code.

<?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'};
?>

I hope my question is clear enough.

9
  • 3
    look at your form method very carefully and the array for $string. I'd call that "undefined index..." Commented Oct 8, 2015 at 14:51
  • An input of type=textarea? Commented Oct 8, 2015 at 14:53
  • your form tag is having name attribute 2 times Commented Oct 8, 2015 at 14:53
  • 1. what is your controller? 2. check your firebug console... are you seeing any error under network? 3. change to action="/youtubev3.php" method="POST" Commented Oct 8, 2015 at 14:54
  • your form won't be submitted as you have just a button not a button with type 'submit' Commented Oct 8, 2015 at 14:55

3 Answers 3

1
  1. Your method is GET but you're looking for $_POST.

  2. Your syntax is very messy. Ensure that each HTML tag - including <form>, <input> and <button> - has only one name attribute and id attribute.

  3. To post the data, change the button type to type="submit" instead of "button".

  4. Use <textarea> instead of <input type="textarea"> or, better yet for one-line inputs, use <input type="text">.


Full, corrected code

<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>
Sign up to request clarification or add additional context in comments.

7 Comments

he is also using the name attribute two times
he also need to change his button type as submit instead of button
He has a lot of syntactical issues with his code, but even with perfect syntax, the script won't work unless the form method and retrieval method are the same.
a textarea is not even needed. For what he needs it seems that a normal <input type="text" /> would suffice
That's not for us to judge... If he wants a textarea (as he's written), he's gone about it the wrong way. Whether or not he needs a textarea, well... Regardless, I've added your suggestion to my answer.
|
1

In your PHP file you are trying to get the URL in $_POST while from the form you are sending the data through get method.

Also the submit button has two name attributes.

<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>

Comments

0

Check your html code. It contains lots of spacing and errors. Also the input type should be submit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.