Edit form with hidden inputs - php

I have this simple form.
a.php
<html>
<head>
</head>
<body>
<?
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'text' name = 'name'>
<input type = 'submit' value = 'SEND' name = 'send'>
</form>
";
?>
</body>
</html>
a2.php
<?
$name = $_REQUEST ['name'];
echo $name;
echo "
<form action = 'a2.php' method = 'post'>
<input type = 'submit' value = 'EDIT' name = 'edit'>
</form>
";
?>
How can I keep the value introduced when I click EDIT and i'm back to the first form?
Thanks.

EDIT 2: using hidden inputs
on a2.php just put another <input type="hidden" name="hidden_name" value="{$_POST['name']}" /> after you hit submit on a2.php (BTW for it to go back to a.php, you need to change the form action="a.php" on a2.php), a.php will have a $_POST['hidden_name'], that will contain the value from the first iteration.
EDIT: before you start handling $_SESSION variables, first initiate the session before any html output with a session_start() function.
Use a superglobal like $_SESSION so in your case you would need to fetch the incoming in a2.php $_SESSION['name'] = $_POST['name'] and refer to the $_SESSION['name'] in your a.php. Remember that $_SESSION['name'] will retain the last assigned value until the the session is terminated, I.e. browser window is closed.
You can read more in http://www.php.net/manual/en/reserved.variables.session.php
Also about session_start: http://www.php.net/manual/en/function.session-start.php

1.creating the hidden inputs in forms by using type="hidden"

Related

Php POST method to 2 page

I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];

PHP - store input variable for static reuse

There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.

Pass variable from one page to another, then discard it

Is there a way I can pass a variable from one page to another but it only work on the next page clicked? After that it can be discarded/destroyed.
I've tried a php session but can't seem to kill the session on the next page clicked... or even if that way may be the wrong way to approach it.
Here's my session code:
<?php
session_start();
$x = $category;
$_SESSION['sessionVar'] = $x;
echo "$x";
?>
<?php
session_start();
$x = $_SESSION['sessionVar'];
echo "$x";
?>
I want to do this without having to submit a form.
You can pass a variable via a php session and then unset the variable on the following page.
$_SESSION['my_var'] = "some data";
$_SESSION['my_var'] = null;
unset($_SESSION['my_var']);
Send it as POST:
<form action="nextpage.php" method="post">
<input type="hidden" name="yourvariable" value="12345" />
<input type="submit" name="submit" value="Next page" />
</form>
Is it what was supposed?

How to get url id from another page by form action page

i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.

How to catch & append a textbox value to a URL in this PHP code?

I managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?
Here's my code:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
?>
<script type="text/javascript">
var val1 = document.getElementById('val').value;
</script>
<?php
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
<form method="post" action="">
Enter value: <input type="text" name="value" id="val" /><br/>
<input type="submit" value="Submit" name="submit" />
</form>
</html>
Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)
As far as I understand your question, what you need to do is:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
$val1 = $_POST['val']; // TO store form passed value in "PHP" variable.
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
} // This is to end the "if" statement
?>
and then change the form input field to:
Enter value: <input type="text" name="val" id="val" /><br/>
I am not sure if POST accepts id values too!
Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.
Then change your PHP code to this:
if(isset($_POST['value'])) {
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}

Categories