How do i make a form with an image and text - php

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.

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

posting your data to a html page passes the (post-)variables from form fields to this page, but it doesn't save the data.
without saving the data you are only able to display the text from the form on the next page for the same user. and whenever s/he leaves the page, it's gone.
i guess that's not what you wanna do, so
you need to save the data in some way, most commonly using a MySQL database.

Related

Using PHP to insert form values

So I have a website on which users can share content. I would like to add a like button to user posts. What would be the smartest/cleanest way to do it?
Right now, I know I could use a submit button with two hidden form fields with values containing the post id and the user id of the poster. Then I would just use my $_SESSION['id'] to insert that into a new row in the LIKES table. Now, that's not very effective. I mean it work and all, but I want to do it so that It is instantly updated both on the page and in the database. What is the logical process to it? Could someone lead me in the right direction as to what I need to learn? Is it javascript? Ajax?
By users I assume you mean registered users with an active session.
To be honest, I know that the best way to do this is ajax as #DarkXphenomenon sugested, but I don't know how so I will put my approach in plain php.
<?php
if (is_int($_POST['postnumber'])) {
mysqli_query($connected,"INSERT INTO likes (userid, postid)
VALUES ('".mysqli_real_escape_string($connected,$_SESSION['userid'])."',".$_POST['postnumber'].")");
echo "Liked";
} else {
?>
<form>
<input type="hidden" name="postnumber" value="3456">
<input type="submit" value="Like">
</form>
<?php
}
?>
NOTE: $connected should be a link as seen in mysqli_real_escape_string() and mysqli_query()
PS, I don't know what else you want to achieve. This code will show 'like' instead of the submit button when you click on it for a single post (only the first time you submit it). Work with it, you can try to think how to adapt the code for several posts, how to count the total number of likes for each post, if you want to display when mouseover which users liked it, add the ability to remove the like, anything you want. That's up to you.

Why is PHP template not retrieving correct data from mySQL database?

Okay, so I have three pages:
index.php
form.php
posting.php
It's basically a jobs posting site, where users submit data via the form (form.php), that form data gets put in the database, then I pull that data out into a table on the home page (index.php). Then, if you click on one of the rows in the table, you get taken to the job posting itself (posting.php). It's a lot like a message board. You can see the live version here: You can see the live version at http://onepotcooking.com/students/mattsellers/final_project/final_project.php.
So the problem I'm having is that the job title links in the table are not leading to their corresponding job postings. Every single job title link in the table leads to the same row in the database...that is, the row with id = 1. You'll see that if you click on a job title, the URLs are dynamic...but it just seems that it's not pulling the correct data. I had it working the other day, but for some reason it's not anymore...it's a bummer.
The code I have around the job title links is:
<div class='job_title'>
<span class='job_title_style'>
<a href="posting.php?id=<?php echo $row['id']; ?>"><?php echo $row['job_title'];
</a>
</span>
</div>
Shouldn't that do it? Any help you could give me would be greatly appreciated.
Thanks!
-Matt
Matt, just thinking. As far as I understood, the link to the offer is put in MySQL also while form is submitted, so you may define the ID part of the link as variable there.
If you do not have a column for the URL in MySQL table, and no query is runing on index.php, just posting the code echo $row['id'] will not give you the id of the offer.

Using objects instead of GET

I'm working in PHP to allow users to delete content and I want "delete" to be displayed in text(like a hyperlink) instead of a submit button, however, I don't want to use an actual hyperlink. Think how in most forums when you delete a comment it's generally text saying "delete comment" instead of a submit button.
I'd like to avoid using GET in this case because it's for deleting content. I understand OOP so I don't need an explanation of how it works.
Is there any way to do this with text saying "delete" (not a button) and then use objects to pass it to a function, instead of using a hyperlink to pass values?
Here's what I'm currently using to display the link, however, I use get to retrieve the values, which I would really like to avoid.
<td><? echo $request['username']; ?></td>
<td>Delete</td>
Since GET is to get/retrieve data, you should use POST to POST updates to the server (i.e. deleting a record).
You can either use a submit button, and style it to look like a link
in CSS (best)
Or you could use an actual hyperlink and use JavaScript/jQuery to submit it for you to the server (not ideal).
I suggest you use the first one if you must use links. However, there's nothing wrong with using <button>s:
Semantically, hyperlinks are supposed to lead the user to a different page, where a button is supposed to make something happen.
It makes more sense to use a button rather than a link in this case.
Please consider that.

Is using an HTML Form the ONLY way to post a $var to $PHP_SELF?

IS there a way to post a $var to $PHP_SELF so that I can use it further down the script?
After 2 hours reading dozens of questions which helpfully appear in the sidebar to the right,
it became apparent that they pretty much all assume an HTML Form has been / will be
activated.
But psuedo~code of what I need looks more like this:
< php
$someVariable=y;
$otherVar=X;
// and the usual setup for accessing the `$_POST` of php:
$HokeyDino=`$_POST`["SendOFF"];
$SendOFF=101;
// etc. and then come to a point where I need the script to just automatically post a value
[ the lack of knowledge ]
// which if I had tha codez!
// would permit the use of that $var, $HokeyDino ...
if($HokeyDino==100){
// do stuff
}
I don't like looking foolish, but gotta ask away, because I figure I have missed learning some elementary aspect of programming, being self-taught so far, but not knowing what might be lacking makes it hard to go look productively.
Thanks very much!
EDIT // Clarification.
Wow, this is amazing. half an hour, 24 people reading the question. Blows my mind.
Right. What I have gotten done so far to give more background:
A php script which uses fopen to create on the fly another php / html page, and all the
code on the Authouring originating script, to write (a+) to the newly created temp page, the whole thing.
From a loop on the authouring page, I have code for retrieving POSTS I send TO that temp page, and that code gets written to a very temp page... then I cause the first part of the page to be written, to get placed on the Temp page, by put_contents etc.,
Next, from another loop on the Authouring page, I write code which item by item matches the things which were included in the < head > of the Temp page.
Anyhow, without graphics, it's a bit tough to explain. What I have at the point I have gotten to so far, is the newly created/assemble Temporary page, can be accessed as a WebPage,, and a button click on it, will successfully POST a value back to the originating/Authouring script.
Here's the tricky part: There isn't any way I was able to devise, to dynamically create code ON THE AUTHOURING page, to recieve POSTS from the Temp Page.
But I realized that if, in the Loops on the Authoring Page, I was able to $PHP SELF post a
string which would be the code for creating a * $Var = $ POST; to catch the values from button clicks on the TEMP page, it would work.
Critical, is that the Authoring Page, doesn't know how many buttons will be made over on the Temp Page ~ that depends on the number of items in the database, which the loops are reading and translating into code which builds the Temp Page.
So, there is no way to hard~code, on the Authouring Page, all possible code for recieving posts, but I could use one Universal $Var= $ POST[ X ] if I could generate it on the fly in the loop, on the Authoring Page.
Hence the need to write code which will $SELF POST, and have it triggered just by normal programme flow, and not the click of a button in a form.
Hmm.... clear as mud yet? :) the question still is pretty straight foreward.
Cheers!
//// Loop
Create CViewerTemp
read DB and manipulate data
Loop B
create, and write to VeryTempHead page
code which creates the top of CViewer, HEAD items
create, and write to VeryTempBody page
code which will work there, items one by one matching head items
end Loop B
Write code which is 1ne time only stuff, to begin CViewer.
then transfer the stuff from VeryTempHead page, into CViewer, kill
VeryTempHead
then transfer the stuff from VeryTempBody to CViewer, kill Very Temp Body.
Open CTempViwer, click on a Button, a value gets posted to Authouring Page.
Authouring Page doesn't recieve anything, no code to do so exists [YET! :)]
If you want to create data on the fly, but not from $_POST, you can just populate $_POST from any other source like this:
<?php
// some calculations
$_POST['my_var'] = $some_calculated_stuff;
// later in your code
if(isset($_POST['my_var'])) {
// works as if it had been posted
}
?>
Is this what you're looking to do?
It's a bit hard to follow, so I'll suggest another potential:
Have you taken a look at cURL?
I think you mean hidden input fields. You can print them with php and they will be posted to your next php script.
Please note, that the user can change the values and you shouldn't trust them.
Also, you can consider using $_SESSION. That would be the better way to solve the task.
Please note that you shouldn't use PHP_SELF because it's insecure.

Change some user settings, but not all from a single form (PHP, MySQL)

I am creating a site in PHP while storing my date in a MySQL database. I have already created my sign-up, login, logout portions, but I would like to make things more user friendly and add an area where people can change their user settings. Some of the settings that I would like for the user to be able to modify are as follows:
full name
email
age
gender
etc.
That being said, I would like for them to be able to fill out only the portions of a form that they would like to update. They should be able to leave everything else unchanged and submit all of their changes with a single submit button.
Any suggestions as to the best way to approach this problem are greatly appreciated.
As a side, I would eventually like for this site to contain AJAX (where the user might be able to select individual settings and change them at will), so if your solutions take that into consideration, that would be great.
EDIT:
Sorry, but I should have mentioned that I want to keep the information from being shown to the user (i.e. displayed in the text field) unless they explicitly type in there. As far as I can tell, this would keep me from always posting all of the data every time.
I have a great way of achieving this. Just simply do some if/else coding in php. Like this--
Html Code:
<form action="settings.php" method="POST">
<input type="text" name="full name" />
<input type="text" name="email" />
........ (and more)
</form>
And PHP code ---
<?php
if($_POST)
{
if(isset($_POST['full name'])) { //Update full name of user and redirect to the settings page on success. }
else { //Redirect and show errors! }
if(isset($_POST['email'])) { //Update email of user and redirect to the settings page on success. }
else { //Redirect and show errors! }
}
?>
Or you can use array function of PHP to set the MySql queries in it like ---
<?php
mysql_query("
UPDATE table name SET
//Loading different values from the array using foreach() php function.
");
?>
Just try to do some modifications in it.
Hope this helps you.
Your choices are:
1) multi-stage edit process. 1. pick fields to change. 2. present fields for editing. 3. save updated data. Rather tedious
2) present all the fields in a form, but use some Javascript to keep track of which ones were changed and submit only those
3). present all the fields in a form, and update all the fields in the database. if one or more weren't changed, the update is a null-operation anyways.
3)'s easiest to implement. 2)'s dependent on javascript. 1)'s tedious for you and even more tedious for the user.
As long as you do proper validation on all the fields, I don't see how #3 is anything but the most logical choice.
Create a edit.php or something else. Create textfields which one you want to edit.
All information could be showed thats related with the unique id of a user.
input type='text' value='"Example: Select * from users WHERE id = '$userid'"' name'Example: Update_name'
Do this for all the field you want to edit. After creating this edit.php. Use a script to update the $_POST or $_GET user details. Based on this easy to use script you've got a edit function for a user.

Categories