Form to let users post a youtube video

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Form to let users post a youtube video

Post by stijn22 »

Hi,

In my website (dht-clan.tk) I use this link for a youtube video:

Code: Select all

<BR><a class='example6' href="http://www.youtube.com/embed/fMBj5phmdJM?rel=0&wmode=transparent" title="Black Ops: DHT# hanoi HCTF">2: Black Ops: DHT# hanoi HCTF</a>
Then a pop-up from colorbox will show up, and the video plays.
What I want now is that user can fill in a form where the put the title of a Youtube video and the link (for example: http://www.youtube.com/watch?v=fMBj5phmdJM). And when they click "submit" a line of text will be created as the above, and then it needs to be posted in the file "uservideos.php" under all other videos.

But I have no idea how to do that :oops: I hope someone can help me.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Why are you saving in a file and not the database?

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Actually, that's a real good idea. So then it will save the title and link in the database. But how would the script that generates links from that information look then?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Before I can even begin you need to post what you have already otherwise what I type might not work.

Just post the form and php you already have and I can go from there.

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Ok, I have now this code for the form:

Code: Select all

<form method="post" action="">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td>Title:</td></tr>
<tr><td><input type="text" name="Title"></td></tr>
<tr><td>Link:</td></tr>
<tr><td><input type="text" name="Link"></td></tr>
</select></td></tr>
<tr><td>
<input type="Submit" name="Submit" value="Submit">
</td></tr>
</table>
</form>
And I have a database named "uservideos", with the colums: Id (auto increnment), Title, Link.

The connection details are stored in a file which I open at the beginning of the php file.

I don't have any php code yet.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Ok, show me the connection file you are including at the top with all other details just to make sure. You can star the actual passwords, usernames, server hosts and database names out.

So have you not had a go at the php handler yet for the form or have you ever connected to a database before etc?

Are you a beginner so I know whether to comment the actions or not?

You mentioned that your users are placing these video links within the database so does that mean you need the links assigned to a certain username etc and how do you want to call your video links etc.

These are questions that you need to decide before you actually start coding.

Just let me know what you want to start with and I can help you through the code.

For starters you need an action in your form to handler the user inputs:

Code: Select all

<?php action="insert_video.php" ?>
Then you need to call the values once they have been submitted to the insert_video.php page:

Code: Select all

<?php
$username = $_POST["username"];
$title = $_POST["title"];
$link = $_POST["link"];
$other1 = $_POST["other1"];
$other2 = $_POST["other2"];
$other3 = $_POST["other3"];
?>
Best wishes
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

So basically you would have your insert_video.php file:

Code: Select all

<?php 
// Database: uservideos
// Table: ????????
// Columns: Id (auto), Title, Link.

require_once("includes/connect.php"); // Your database connect file in a directory outside your root called 'includes'

// Gather the user input values
$Title = $_POST["Title"]; // Grabs your users Title
$Link = $_POST["Link"]; // Grabs your users Link

// Set error location link values for Title & Link
$error1 = "video_submit.php?e=1"; // Sets the string value for error redirect on Title
$error2 = "video_submit.php?e=2"; // Sets the string value for error redirect on Link

// Check to see user values have something in them
if (empty($Title)) {
		header("Location: $error1"); // Direct them back to your form with error Title missing
} elseif (empty($Link)) {
	    header("Location: $error2"); // Direct them back to your form with error Link missing
} else {
	
// Insert the values 'Title', 'Link' in your columns within your database (uservideos) table (????????) 
        $insert = "INSERT INTO ???????? (Title, Link) VALUES  ('$Title', '$Link')"; // Create string of required actions
		mysql_query($insert) OR DIE (mysql_error()); // Saves values in to database or stops on mysql error

	    header("Location: confirm.php"); // redirects to confirmation page
}
?>
Then you would have your video_submit.php file:

Code: Select all

<?php
$e = $_GET["e"]; // Gets the error code if insert_video.php sends user back because of error
?>
<form method="post" action="insert_video.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td>Title:</td></tr>
<?php if ($e == "1") { echo "<p>Please enter a title"; } ?>
<tr><td><input type="text" name="Title"></td></tr>
<tr><td>Link:</td></tr>
<?php if ($e == "2") { echo "<p>Please enter your link"; } ?>
<tr><td><input type="text" name="Link"></td></tr>
</select></td></tr>
<tr><td>
<input type="Submit" name="Submit" value="Submit">
</td></tr>
</table>
</form>
Then you would need a confirm.php file:

Code: Select all

<?php
echo "Thank you for your video link submission<br/><br/>";
echo "Would you like to submit another video <a href=\"submit_video.php\">YES</a>";
?>
That would be the basics of your setup for getting the information from the user and then checking to see if there are actual values submitted. If so then they get entered into the database and if not they are redirected back to the form to try again. When complete they have option to submit another video which directs them back to the submit_video.php from your confirm.php page.

You did not let me know the table name so where I have place ???????? you need to place your table name.

I hope this gets you started.

Best wishes
Last edited by phphelpme on Tue Sep 06, 2011 4:42 am, edited 1 time in total.
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Wow, thanks for these codes! I will answer your questions:

Ok, show me the connection file you are including at the top with all other details just to make sure. You can star the actual passwords, usernames, server hosts and database names out.

Code: Select all

<?php
$host = "mysql11.000webhost.com"; //database location
$user = "********"; //database username
$pass = "********"; //database password
$db_name = "a3612771_db"; //database name

//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

//sets encoding to utf8
mysql_query("SET NAMES utf8");
?>
So have you not had a go at the php handler yet for the form or have you ever connected to a database before etc?
I'm connecting a lot of times on my website, I know how that works. But I've never used a php handler before.

Are you a beginner so I know whether to comment the actions or not?
I think I'm not a beginner anymore, but you can't call me advanced either. Commenting the actions will help me alot with learning :)

You mentioned that your users are placing these video links within the database so does that mean you need the links assigned to a certain username etc and how do you want to call your video links etc.
No, they do not have to be assigned to an username. The way I want to call the liinks is just the title the users have given.

You did not let me know the table name so where I have place ???????? you need to place your table name.
Sorry, I wasn't clear. The table name is uservideos and the database name is in connect.php
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Thanks for getting back to me.

You are very welcome for the coding above. I was just striking in the dark really not knowing everything that you required and that is one of the main reasons I commented the coding so you could add things to it just incase you did indeed have to assign the video links to each user account.

My only advice about your connection file would be maybe assign it into a 'function' then call that function with one line of text everytime you need to connect to the database a couple of times maybe in a very long page of coding.

Saves you leaving the connection open while all the PHP coding gets executed because you can also assign a databaseclose() function maybe which again would allow one line of code to close your database connection. This will help if indeed you are connecting many times to your database throughout your site.

If you would like an example of this also then just say the word and I will make one up with your details in it.

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

I think that the code is not yet finished completely. I also need a code that makes links from the info from the database. Like the link in my first post. And then puts all the links under eachother :D I hope i'm not asking too much :P
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

So can you just confirm to me exactly what it is you require?

I am guessing you want a code that connects to the database again and grabs all the 'Title' and 'Link' fields and then displays them on a webpage so people can click them yeah?

If you just confirm that is what you want then that is not hard at all.

You also need to confirm what class='example6' is in your first link at the top because does this mean you need the value of your css class to increase with your listed video links? or does this not matter and you need them all to be the same number etc?

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

I can confirm that. Example6 is something from colorbox. If I use that, and users click a link, a box shows up, instead of opening a new page for the video. So all the links need to be example6.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

I have put together a basic code that will look into your database and extract all your Link(s) along with the Title(s) and display them on your page within a table.

You will have to change this code to make it fit within your current page as I assume this is for a clan website you have and you are allowing your team members to insert video capture links of their gaming experiences from a youtube account.

This should be your display_links.php page:

(Dont forget to include your database connection file at the top)

Code: Select all

<?php
// Build query to get results of video Title & Link 
$result = mysql_query("SELECT * FROM uservideos") // Select all results from table uservideos
or die ('cannot select table:' .msql_error()); // If error then stop and display mysql error code

// Begin to build table for sql results
echo "<table border=\"1\" width=\"100%\" >"; // initial table width 100% of page
echo "<tr><th width=\"50%\">Title</th>"; // Title width share is 50%
echo "<th width=\"50%\">Link</th>"; // Link width share is 50%

// Display results in your table
while ($row =mysql_fetch_array($result)){ // Fetch your results and place in an array to call each value
                
        // Make sure you include your css file for your class="example6" to work with the popup function for each video    
        echo "<tr><td>";
        echo $row['Title'];
        echo "</td><td>";
        echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\">".$row['Title']."</a>"; // Includes your CSS class
        echo "</td></tr>";
        }
		echo "</table>";
?>                      
With this particular example you will get a list of all your videos Title & Links in a list all in one page. You might realise that if you end up having many many links included in your database then you will have to consider pagination techiques so you can show X results per X pages.

I suggest you look into this for the possible near future.

A good example of this would be:

http://www.phpjabbers.com/php--mysql-se ... php25.html

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Thanks, I'm going to test it by the end of the next week, before that I don't have any time. But I have a question, can you make the form, that it will ask users to put in their names, and then the link will be showing like this on display_video.php:

Title here By Stijn22

And Stijn22 is ofcourse the name the users have typed in the form.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Hi again,

You have all the code now to add anything you want. lol I dont need to do it for you anymore. If you want to add an additional field for the user to fill in such as Username then just add that to the form, then call that POST name in your handler for the form, and you just need to add the field into your SQL insert code. Dont forget you will also need to add that field within your table in your database.

All you are doing to achieve what you want is copy and pasting the code I have already given you and changing the string values thats all.

Best wishes
Post Reply