Reputation: 4041
I'm trying to write a page that allows users to type a post and, when submitted, opens a new page with the post on it. This means that the post must be inputted in a database first before it can be retrieved on another page. This is similar to when someone asks a question on stackoverflow. the question appears on a new page and the page is given a unique id, except i would like this unique id to be in a get variable.
HTML of current page (ask.php):
<form method=POST' action='ask.php?q<php echo $id ?>'>
<input type='text' id='post'>
<input type='submit' value='submit' name='submit'>
PHP:
$post=$_POST['post'];
//then run query to input data into database
}
$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
while ($row = mysql_fetch_assoc($result)){
$id=$row['id'];
}
ask_action.php:
<?php header("Location: http://localhost/biology/question.php?q=$id"); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
include '../connect.php';
if (isset($_POST['questionSubmit'])){
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','biology','0')");
$id=mysql_insert_id();
}
?>
</body>
</html>
Upvotes: 0
Views: 3414
Reputation: 544
If you don't want the "ask" page to be on the same page that you view the post, you can split this in to three pages.
Page 1: The page with the form where people writes their post and submits. Page 2 will be in the action part of the form.
Page 2: The page that retrieves the POST data. Here you'll verify all the information and submit it to the database. You then retrieve the insert id and redirect the user to the page where you want to display the results. You use the id from the insert query in the redirecting.
Page 3: Where the users see the submitted information.
I notice that you've tried this approached somehow, but you've got a few mistakes that needs correcting. First of all your ask.php page needs improvement.
You're missing one apostrophe ( ' ) before the POST in method, and your action needs correcting. Remember that the form action is supposed to go to the page that verifies and handles the data from the form. In this case it would be the *ask_action.php* page. Therefore the ask.php page should be like this:
<form method='POST' action='ask_action.php'>
<input type='text' name='question' />
<input type='text' name='detail' />
<input type='submit'> value='submit' name='submit' />
</form>
*ask_action.php* will handle the data, verify it and redirect to the page that views it. There is no need for html on the page that verifies it.
<?php
include('../connect.php'); // your database connection etc.
if(isset($_POST['submit'])) { // only react if the submit button is pressed
$question = mysql_real_escape_string($_POST['question']);
$detail = mysql_real_escape_string($_POST['detail']);
$date = date("d M Y");
$time = time();
$user = $_SESSION['id'];
mysql_query("INSERT INTO questions VALUES('', '$question', '$date', '$time', '$user', 'biology', 0)");
$id = mysql_insert_id();
header("Location: view_page.php?id=$id");
} else {
echo "Nothing submitted.";
}
?>
Then you'd have a third page where you display the data that you get from the database. If you notice the header function that I've used, it's redirecting the user to view_page.php. This is where you'll display the data by the id number that is supplied. To get the id number you simply use $id = $_GET['id'];
.
I also noticed that you're using both time() and date("d M y"). That is not necessary. If you read about time on php.net you'll see that the time function generates the current unix timestamp. You can use that to output a date in the way that you want to. For instance: if you'd like to display both the date and time that the question was submitted you can use this date("d M y H:i", $time)
where $time is the time-column in your database table.
This can all be combined in one single page, but I kept them separated so it's easier for you to see the difference.
Upvotes: 1
Reputation: 106
The first problem here is in your opening form tag. It should read:
<form method='post' action='ask.php?q=<?php echo $id ?>'>
or
<form method='post' action='ask.php?q=<?= $id ?>'>
Your input tag should read:
<input type='text' name='post' id='post' />
I think the functionality you're looking for is provided by mysql_insert_id
, as long as your id column is auto-incremented. If you're not auto-incrementing your id column, could you provide more info about your db table?
Upvotes: 1
Reputation: 18531
On your "submit" page, you need to have the PHP code do the insert into the database then retrieve the ID of that new post using mysql_insert_id. Then that page could redirect the user to the appropriate page using the just-retrieved ID as a GET parameter (using the header function)
Upvotes: 0
Reputation: 2100
You have a few problems here. First and foremost you NEED to synthesize your incoming code with something like mysql_escape_string($_POST)
. You also need to name your text field, currently you're trying to pull it based on ID and that won't work
Change: <input type='text' id='post'>
into <input type='text' name='post' id='post'>
Your order is backwards as well. Without using AJAX the best way to do this is to load your posts & id's using PHP at the top of the page, then have the actual writing to the database happen on the resulting page.
Upvotes: 0