I have this kind of href:
DOWN</td>
When I click on the links I lost datas entered before in a form,what is the way to prevent this?
Thanks!!!
You can either set a cookie:
setcookie('savedData' , $whateverYouWantToHaveSaved, time()+60*60*24*7);
or use a session
session_id();
session_start();
$_SESSION['savedData'] = $whateverYouWantToHaveSaved;
Ref:
cookies: http://www.php.net/setcookie
or
sessions: http://www.php.net/manual/en/ref.session.php
From there you will recall your data via either:
$savedData = $_COOKIE['savedData'];
or
$savedData = $_SESSION['savedData'];
The rest you will need to learn on your own.
You could use a hidden textbox inside the form and when you click on the link you call javascript function to set the value of the hidden input-field and that function submits the actual form. After that you could access the hidden input value from PHP.
DOWN</td>
Related
i am having form with many text inputs in a page
along with them i am providing zend pagination to select results.
but when i use zend paginate the user form inputs are lost as it is not submit
as the page reloads evertime i move to a new page is there any way i can maintain the user input .
Please can any one help me find a solution to this problem ..
Pseudo Code:
With javascript get the values on change and store it in a cooke (use jquery cookie plugin)
jQuery('form').on('change', 'input', function() {
jQuery.cookie(...)
})
And the solution is...
AJAX + Session
Send a ajax request on change event of every input.
Save data to Session.
Every time you load form data check for Session to fill form fields.
You can use sessions:
$form = array();
if (!empty($_POST))
{
$form = $_POST;
}
elseif (!empty($_SESSION['form']))
{
$form = $_SESSION['form'];
}
And then use $form to populate the input fields.
You could use ajax onfocusout of elements and store the value in the session.While loading the page fetch the data from the session and show it.
Could anyone please tell me how to use php session variables inside a JavaScript function? I have a php page with some option buttons and when the user clicks the submit button i want the value of the checked option button to be stored in a php session variable which i want to use on another page. which radio button has been selected is being checked through JavaScript. In the same function i want to store that value in a php session varaible.
Could anyone please tell me how this can be done?
If i use the session variable inside the javascript function it simply ignores it.
you could get the php session variable name in javaScript like this
var varname = '<?php echo $_SESSION["variable_name"]; ?>';
there is something called "Javascript Cookies", very handy for this kind of stuff came in good use when i had your problem.
here's the link: Javascript Cookies
hope it helps you out!
I have form inputs to send to another page to check on and validate them, and in case of errors I have to redirect the user back to the page where he/she filled the form.
So I need to send the content of $_POST back to refill the inputs to avoid refilling them manually...
How can I do that??
Here are two options:
1) Have the PHP script redirect back to the page with the form, with the form values in the URL as GET variables. You can then use those to refill the form.
2) You can send a POST request via jQuery without requiring the user to leave the page. See here: http://api.jquery.com/jQuery.post/ . With this, you can check the user's input in the PHP script, and only redirect them if their input is valid. This saves you the trouble of refilling the form. You can use it like this:
$.post("myphpscript.php",
{ someData: someValue, someMoreData: anotherValue },
function(returnData) {
// do stuff here on return
});
In myphpscript.php you can get the post values as $_POST["someData"] and $_POST["someMoreData"]. The function is what happens when the PHP script returns, and returnData is what the PHP script echoed.
If I'm correct you want to check your POST-values and if they are invalid input, you want the user to be directed back to the original page and fill the input-fields with the POST-values.
<input type="text" value="<?php echo (isset($_POST["text"] ? $_POST["text"] : "") ?> />
This checks if the POST-var is set, if so, it sets the value of the input-field to the value of the POST-var, else it sets the value to empty
An alternative to excellent answers posted is to store these values into the session and access them from there.
I have profile page where the profile is retrieved via GET. The index file has this:
$profile = $_GET['profile'];
When I log in on the profile page, the $profile variable disappears. Here is the form action on the login function:
<form name="login-form" id="login-form" method="post" action="./index.php">
(The $profile variable is separate of the login username.)
How could I make the page retain the $profile variable?
Thanks in advance,
John
You should use a session.
Set the variable in the first page and retrieve it in any other page.
GET and POST variables aren't preserved throughout. Only from one page to another. Unless, you specifically GET or POST the variables from page to page.
Set it in a session $_SESSION['profile'] = $_GET['profile']; and retrive it $profile = $_SESSION['profile'];
Here's a simple tutorial Session in Action
If you need to retain query string data through a form, use a hidden field:
<input type="hidden" name="profile" value="<?=$_REQUEST['profile'];">
The method="post" part of a form does not send variables the same way as a GET. Instead, it posts them in the background, invisible to the user.
If you wanted to keep the variable - you could do one of two things:
Change the form to method="get".
Change the action of the form to action="./index.php?profile=<?php=$profile?>"
The variables passed from this form is accessible only in the "index.php" page. You will be able to access it as $_POST['profile'] or $_REQUEST['profile'].
If you want to access this in all your pages you have to use session.
You need to assign the value to $_SESSION['profile'] = $_POST['profile']. After this you will be able to use $_SESSION['profile'] in all pages. Before assigning value to $_SESSION['profile'] you need to add session_start(); to the beginning of the code.
You can use unset($_SESSION['profile']); to remove value from the session variable
I have an HTML form. Let's say I fill out all the fields and submit it (PHP script runs here).
Then I want to go back to the form using "Back" button of my browser.
What I see is the empty form.
What do I do to have the entered data retain on the page after I come back to it using "Back" button of the browser?
Thank you!
If you use the "Back" button of your browser then your browser is responsible for re-populating your form data.
Usually that functionality is handled by the browser, however if you want to "force" the fields to always be pre-filled with the user's data, you can store the $_POST data in a session variable and use that to load the form.
Example:
// submission page
session_start();
if(isset($_POST)){
// save the posted data in the session
$_SESSION["POST"] = $_POST;
}
Then on the actual form page, you can check to see if session data exists. It won't if the form is being loaded the first time, but it will if the user submits the form and then presses the browser back button:
// form page
session_start();
if(isset($_SESSION["POST"])){
// previous POST data has been saved
// build the form with pre-defined values from $_SESSION
...
} else {
// no previous data
// build the form without pre-defined values
...
}
Note that you must call session_start() before outputting any HTML.
Store the value in a session
session_start();
//form so that you have all the potential forms in a single session array
//form_1 to identify the form in question
if(!empty($_POST)){
$_SESSION['forms']['form_1'] = $_POST;//if this is for the public internet, then I would really consider making sure that the posted data matches the received data... (and that its comming from YOUR form), which is way too long to post here...
}
then on the form page
<input name="flowers" value="<?php echo if(isset($_SESSION['forms']['forms_1']['flowers'])){ echo htmlspecialchars($_SESSION['forms']['forms_1']['flowers']);} ?>" />
obviously the above can be simplified, but for a example's sake it's better this way.
(make sure to clean out the old form data eventually)
You can potentially store the data in the session, and re-populate it back using PHP sessions. You should create a separate back button that takes you to the previous page.
Example:
Storing data:
$_SESSION['data'] = $_POST['item1'];
In the HTML Forms:
<input type="text" name="someinput" value="<?=$_SESSION['data']?>" />