getting value from textbox - php

I have following code:
<input type="hidden" name="qz_id" value="<?=$quizid; ?>" />
<input type="hidden" name="s_id" value="<?=$s_id; ?>" />
<? if($num!=$i) {?><input type="submit" value="Next" /><? } ?>
<? if($num==$i) {?><input type="submit" name="submit" value="Finish" /><? } ?>
This is the part of whole page of code. What i am trying to do is insert these values in database. Can you tell me how can i get value of textbox "answer" and store it in a php variable? I want to insert this value into database once user press next or finish?
Here is the form:
<form id="formresp" method="post" action="respond.php?quiz_id=<?=$quizid ?>" name="register" onsubmit='return ValidateForm(this)'>
Also i triend $_POST['answer'] it is not working

You can also use $_REQUEST['answer'];. I sort of a $_POST and $_GET.

All form variables are stored in either $_GET or $_POST depending on how you post the form.

Related

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Form won't forward $score to next page

When a user takes a quiz, their score is captured in the value $grade, which displays fine on the quiz's home page. But if I change the form action to forward the user to a new page (grade.php), $grade loses its value.
How can I capture the value and display it on the next page? The values for PreviousURL and user_token display...
<form action="grade.php" method="post" id="quiz">
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>" />
<input type="submit" value="Submit Quiz" />
</form>
So I tried this, but it doesn't work...
<input type="hidden" name="grade" value="<?php echo $_SESSION['grade'] ?>" />
on grade.php make sure to start the file with
session_start();
Based on your question, I assume that you did not start the session on grade.php. There is not much information about the file
If you don't start the session the values of $_SESSION get destroyed
You should not save form values in $_SESSION as form values are already in $_POST superglobal.
You can do it like
<form action="grade.php" method="post" id="quiz">
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="<?php echo isset($_POST['user_token']) ? $_POST['user_token'] : '' ; ?>" />
<input type="submit" value="Submit Quiz" />
</form>
So basically it test if there is a value in user_token field it will apply that value other wise it will set to '' (nothing). If you are not familiar with the ternary operator syntax please refer to http://php.net/manual/en/language.operators.comparison.php
Updated Part:
// Your form page goes like
<?php session_start();
print_r($_SESSION); // If there is a session your form should show the value of user_token
// Rest of the script + html
?>
// grade.php
<?php session_start();
// do a print_r to see if you are receving session variables by
print_r($_SESSION);

Form submit to another page and then review or write to a file

I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.

Using POST instead of GET

Maybe I don't understand English as well as I thought. I need some help. I read the other posts but I am still not able to send variables to another page using POST instead of GET. Please help me with this example:
I have two pages. The first one has a query (Users). Then I make a table with the results and using the following code I can send user id to another page where I can edit some information.
First page:
<td><div align="center">
<a href="Users_modify.php?id=<?php echo $row_Users['id'];?>">
<img src="Icons/info-icon.png" width="20" height="20"></a></div></td>
Second page:
$colname_recordset1 = "-1";
if (isset($_GET['id'])) {
$colname_recordset1 = $_GET['id'];}
and after this I can use the variable to make a query.
With respect I ask you for a sample of the first page statement in order to be able to use POST on the second page. Thank you for your time!
You can only use $_POST on a form.
If it is associated in a link, you can use $_GET or you can use $_REQUEST to get the query string.
You'll fill $_POST by using a form:
<form action="…" method="post">
<input type="hidden" value="$id">
…
<input type="submit" value="Save">
</form>
posting it by form
<form action="second page url" method="post">
<input type="hidden" name="id" value="$id">
…
<input type="submit" value="Post">
</form>
First Page
<form method="post" action="page2.php">
<input type="hidden" name="idField" value="<?php echo $id; ?>" />
<input type="submit"/>
</form>
Second Page
<p>
<?php
echo $_POST["idField"];
?>
</p>

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

Categories