I am creating a script and I came across a problem. I am having to use $_POST and $_GET on the same page, which I don't think makes sense.
I get the value for user id using the GET method from another page through a link and I have to input data from a form into the database in the current page.
CODE SNIPPET 1 : (GET METHOD on page page.php)
echo "<td align=center width=90px height=10px><strong><a href='sessions.php?id=".$userid."' style=text-decoration:none><font color='red' size='5pt'>$i</font></a></strong></td>";
CODE SNIPPET 2 : (POST METHOD on sessions.php going to same page sessions.php)
if($_SERVER["REQUEST_METHOD"]=="GET"){
$id=$_GET["id"];
if($_SERVER["REQUEST_METHOD"]=="POST")
$suggestions=$_POST["suggest"];
Can I get the userid from the url and the POST the values from the form in the same page?
If not, is there a way I can do this better?
You can use one hidden input field in your form in session.php and store $_GET['id'] in value attribute of that input field and it will be pass with your form submit to session.php <input type="hidden" name="id" value="<?php echo $_GET['id']">
you can get the value from GET by adding the following code in sesssion.php
$id="";
if (isset($_GET['id'])) {
$id=$_GET['id'];
}else{
$id="";
}
then in the form located at session.php place a input field like following
<input type="hidden" value="<?php echo $_GET['id']" name="suggest">
then add the following code in the same page
if($_SERVER["REQUEST_METHOD"]=="POST"){
if($id){
$suggestions=$_POST["suggest"];
// run your sql query for insert.
}
}
Related
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
If this is for validation checks no need for sessions as suggested.
User fills in the form and submits back to self
Sever side validation fails
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
If $post is a normal variable, then it will never be saved.
If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
Reference: http://php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION['foo'] = $bar
You could save your $_POST values inside of $_SESSION's
Save your all $_POST's like this:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
Actually in html forms it keeps post data.
this is valuble when you need to keep inserted data in the textboxes.
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>
put post values to session
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.
An little example:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.
I'm collecting an argument from a signup URL using '?' like this,
.../signup.php?id=6489
now i'm collecting this 'id' in my php in this way,
<?php
$id = $_GET['id'];
// echo $id;
?>
Now i want to use this $id in another PHP file - code_exec.php where there is a mail() function which emails this and other parameters collected from a form. Now the parameters collected from the Form work great and they all show properly in the email. However this ID variable does not. I have included the signup.php in code_exec.php using this,
include 'signup.php';
and yet it doesn't seem to work. What am I doing wrong here?
From what it sounds like, you go to signup.php?id=6489, fill out a form which submits to code_exec.php and you want to get the ID in code_exec.php.
If that is the case, you can include a hidden form field in the signup page with that ID.
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
and that will get submitted with the form just like all of the other fields. Make sure to sanitize your inputs!
Without showing what's in signup.php and how you're using that $id value, we can't help. But the obvious one: If the signup.php stuff is working inside a function, then you're subject to PHP's scoping rules and will have to make $id a global, eg.
$id = $_GET['id'];
function email() {
global $id;
blah blah blah
}
// foo.php
$id = '12345';
include 'bar.php';
// bar.php
echo $id;
Will output 12345. Any variables that exist in scope of the include/require call will be available in the included file.
Add a hidden input value to your form and assign it the passed id value, something like this:
<input type="hidden" id="id" name="id" value="<?php echo $id ?>" />
i have a multiple choice quiz which sends a possible answer via URL like this
<form method="post" action="test2score.php?q=<?php echo $quiz_id; ?>">
on the test2score.php page it reads and tries to compare the value like this
$ans = mysql_result(mysql_query('select c_answer from quiz where quiz_id = "'.$_GET['q'].'"'),0);
if ($_POST['answer'] == $ans){
echo "You got it right!";
}else if ($_POST['answer'] <> $ans){
echo "You got it wrong!";
}
where c_answer is the correct answer stored in db, but even when i select the right answer and post it it still echoes "you got it wrong"
any help would be appreciated.
Your form method="post" is post and your receiving values in $_GET['q'] Kindly correct either of the one
Or use
$_REQUEST['q']`
Use a hidden field q and post that value to your action page and receive that value using
$_POST['q'] and use that in your query.
Thanks.
Why are you using a $_GET request in the action of a form that is $_POST? Since you aren't worried about your users seeing the value of q then why not just put a hidden input in the form with the results of q:
<input type="hidden" value=<?php echo $quiz_id ?> name="q" />
Then in your query, check for $_POST['q']
Seems the saner, more logical way to achieve this
instead of passing quiz id from form action pass it from hidden form field
like below
set form action to
action="test2score.php"
and take hidden form field as
<input type="hidden" name="q" value=<?php echo $quiz_id; ?> />
and get that value like below
$_POST['q'];
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>
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.
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?
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.
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)
I've got the following php code printing out the contents of a SQL table.
$query="select * from TABLE";
$rt=mysql_query($query);
echo mysql_error();
mysql_close();
?>
<i>Name, Message, Type, Lat, Lng, File </i><br/><br/>
<?php
while($nt=mysql_fetch_array($rt)){
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file]";
}
}
?>
How would I implement a button so for each "row" if the button is clicked on that row it'll submit the information of that row to another php file?
I want it looking something like...
details details2 details3 BUTTON
details4 details5 details6 BUTTON
details7 details8 details9 BUTTON
details10 details11 details12 BUTTON
Where if BUTTON was hit on row 1 details1,2,3 would be sent to a php file, on row 2 detals 4,5,6 would be sent etc. How would I do this?
Thanks.
it's going to be something like that, depending on the data you need to send:
while($nt = mysql_fetch_array($rt)) {
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file] ".'send request<br/>';
}
}
You can either use GET method and send a query string to the second php page and receive the variables there, like
next.php?variable1=value1&variable2=value2&...
or use POST method by making a hidden form for each row and assign a hidden field for each variable you want to send.
<form method="post" action"next.php">
<input type="hidden" name="variable1" value="value1" />
<input type="hidden" name="variable2" value="value2" />
.
.
.
</form>
or instead of sending all the values, just send the row ID (if any) using any of these two methods and run another query in next.php to get the information you need from database.
Instead of submitting the entire data, just send the ID and fetch the results from the database in the other script. If you want to have an input button, you can do
<form action="/other-script.php" method="GET">
<?php printf('<input type="submit" name="id" value="%s" />', $nt["id"]); ?>
</form>
but you could also just add a link, e.g.
printf('Submit ID', $nt["id"]);
If you really want to send the entire row values over again, you have to make them into form inputs. In that case, I'd send them via POST though.