Search code examples
phphtmlforms

How do i make a form with an image and text


I want to make a form where the user uploads an image, the title with a textbox and a description with a textarea, when the user clicks on the submit boton i want this post to get posted in another page, and if another users makes another post send him to the same page and put his post on top of the older one and so on.

I already a form for the file upload that uploads it to a folder and the form for the text that displays the input in a php page.

I don't know how to continue or if i'm doing it wrong. How do i make the posts go on top of each other? How do i put this image they just uploaded next to their post, i don't think uploading it to a folder is the right way to do it.

Can someone point in the right direction because i'm not a programmer and i cant seem to find a guide on how to do this.


Solution

  • I will try to anwser your question, however you seem to have provided too little information (no code!!)

    There are different ways to work with images, if you say you did it with a folder I am guessing you used a Javascript or PHP of somesort.

    If you used PHP then its no problem. You just place the image AND the text in one variable and echo this variable out. For easy structuring i would use a blind table, although that is NOT the most potimal way and definetly not barrier free, it gets the job done. PHP is very flexible and you can place almost anything into a variable and have it echo out. It would look something like this:

    $output = "
    <table>
    <tr> <td> *image link here* </td> <td> *text link here* </td> </tr>
    </table>
    ";
    echo $output;
    

    Like i said, without any code its a little hard to tell you exactly what you are looking for, but this is a start i hope =)


    Well, the other anwser kinda anwsered it, you would have to save all the data coming in, in a database, preferably mysql since the mysql functions are built into PHP. (i believe however that you are saving the info somewhere since you mentioned a folder with the images)

    Your form would look something like this :

    <form method=post action=display.php>
    <textarea name=comment>
    <input type=submit>
    </form>
    

    Now when someone clicks the button, they are taken to the "display.php" page (or whatever you want to PROCESS the data) Now, included in that page is a statement like:

    $sql_update = "INSERT INTO post (comment) VALUES ('$_POST['comment']'"; 
    

    This statement would insert the information provided in the previous textarea (with the name "comment" into a table named "post" (this table has to be premade though) in the column named "comment"

    Then you save the information into a variable by doing:

    $sql = "SELECT * FROM post";
    $res = mysql_query($res);
    

    This just selects EVERYTHING ( * ) from the table "post" and querys it (mysql_query) meaning it is ready for work

    Then we stick the whole thing in a while loop like so:

    while($row=mysql_fetch_assoc($res))
    {
    $output = "
    <table>
    <tr> <td> *image link here* </td> <td> $row['comment'] </td> </tr>
    </table>
    ";
    echo $output;
    }
    

    This loop will constantly echo every row as long as there is something to echo. $row['comment'] is the link meaning we are grabbing whatever is in the column "comment" in our row and echoing it. (which also would stack one onto the other since it takes on information set, echos it, then takes another information set, echos it ....)

    Now you would do the same for your image, or something similar at least. It gets a little more complicated when we add users and "post-times" to the equation. I dont know exactly how much knowledge you have of PHP/MYSQL but if you wish to process anything with a form, you will need some. I hope my small tutorial did not bore you and i hope it helped you a little bit more =)

    Happy coding =)