I have ran out of ideas for this assignment for a class. I had to create two PHP forms using PHP_SELF and I am suppose to use javascript or jquery to toggle between the forms. I tried using
Form 1
Form 2
This does work on changing the forms, however when I hit the submit button the form it goes back to form 1 and doesn't display the results. The thing I have to figure out is after the form has been submitted using PHP_SELF, how to get it to stay on that form to show the results generated by the server.
You'll need to use something like this
if($_POST){
//display success message here
}else{
//display form here
}
You could add a hidden field to the form called formId and have PHP echo it back, so that you know from Javascript which form's results you're reading:
<?php
...
if (isset($_POST['formId']))
{
$show = 'form1' == $_POST['formId'] ? 'form1' : 'form2';
$hide = 'form1' == $_POST['formId'] ? 'form2' : 'form1';
// This PHP must be inside the Javascript section of the page, at end.
print <<<SETVISIBILITY
\$('#$show').style.display = 'block'; // Should be unnecessary (?)
\$('#$hide').style.display = 'hidden';
SETVISIBILITY;
}
?>
Or you can do the SUBMIT and result display all in jQuery, so that you needn't reload the page losing your form visibilities.
Related
I currently have a form which users can traditionally click on a submit button to save. The form action goes to a page called formProcess.php, which contains a meta-redirect that will save everything then redirect the user back to the form.
I have been requested to add a new feature to a hyperlink on the form page which will automatically save the form before following the hyperlink to a different page. I've currently got some simple jQuery connected to the hyperlink which submits the form like so:
$('#hyperlink').click(function() {
$(this).closest("#save_form").submit();
});
This works and submits the form, but of course formProcess.php has a meta-redirect in it which will take the user back to the form page which I don't want.
I figure if I could pass some extra parameter in the Javascript form submission, like submit('redirect=link'); then I could add something like this to formProcess.php:
if ($_POST['redirect'] == 'link') {
// Redirect 1
} else {
// Redirect 2
}
Is this possible? I can't seem to find any information on being able to pass any extra parameters using submit(); If not, what would you suggest is the best way to tackle my problem?
You can append a temporary field to your form.
$('#hyperlink').click(function() {
$(this).closest("#save_form").append('<input type="hidden" name="redirect" value="link" />');
$(this).closest("#save_form").submit();
});
In your form you can use hidden input type.
Ex.
<input type="hidden" name="redirect" value="link"/>
so when form is submit your hidden field value also to be submit with other parameter.
in your php you can simply do as below:
if ($_POST['redirect'] == "link") {
// Redirect 1
} else {
// Redirect 2
}
You can either use ajax to submit your form and upon success then redirect to the url you want.Without using ajax you could add a hidden field in your form and change it's value.Then server side you ll be able to check the value of you hidden field and act accordingly.
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.
I have an html form that is filled with the values I get from a MySQL Database query. The query is by an id that is sent via GET.
I send the id to the form along with a button that is surrounded by an anchor <a>:
<a href='editRQS.php?id=$row[0]'><button class='edit'>Edit</button></a>
$row[0] is filled by the proper id and I'm sure it's working ok. When I click the button the url is sent carrying the id. It is then received at the other page like this:
<?php
$id = -1;
if(isset($_GET['id'])){
$id = $_GET['id'];
echo "<label class='exists' id='idRequest'> $id</label>";
}
if($id != -1 ){
echo '<form id="findRequest" class="hide" >';
} else {
echo '<form id="findRequest" class="show" >';
}
?>
The problem is that the id arrives at the page - but then it disappears. Any help will be greatly appreciated.
EDIT
The page seems to reload, i have no idea why, i have some javascript that manages some of the page functionality and the only thing that could cause this is an event handler for when the another form is submitted but I have prevented it like this:
$('anotherForm').on('submit',function(e){
.
.
.
e.preventDefault();
}
EDIT
Thanks to everybody that helped, i have yet to find the reason it doesn't work, i tried dissabling javascript and the page stoped reloading so i guess it's another thing that's messing my page up
The page is reloading because the form gets submitted. It isn't enough to preventDefault the form submission. Try e.preventDefault(); e.stopPropagation(); the latter preventing form submission when a submit button is clicked (clicks are still handled, and, in some cases propagate to the form, that is then submitted.
Also, I see you're using jQuery. Just let your event handler return false. jQuery automatically converts a false-returning handler to a e.preventDefault() ||e.returnValue = false e.stopPropagation() || e.cancelBubble = true; construction. Or you could write the code yourself...?
Check your register_globals setting, if it is on you will be overriding $_GET['id'] with the first statement.
You can easily check it by renaming $id to something other then $id.
I'd like to know if it's possible to change the page data will be posted to according on the selections made on the page.
For example, depending on a dropdown, a radio button array etc...
I understand it can be done using javascript, any other options? I'm using PHP and the page data is being submitted to is declared in the "action" property on my form.
Remember that your PHP is built server side, if you want client interaction you will have to re-submit the page. ie. onchange=submit allowing you to use the form data to tell the PHP what to show.
It's pretty standard, JavaScript & AJAX just make it more efficient but essentially its the same thing.
To show another option:
Well, you can decide once the form is posted:
if ($_POST['some column'] == 'some expected value') {
header("Location: http://host/page1.php");
} elseif ($_POST['some other column'] == 'some expected value') {
header("Location: http://host/page2.php");
} elseif ($_POST['some other other column'] == 'some expected value') {
header("Location: http://host/page3.php");
}
Let the form post to itself or a certain page.. then let that page perform the decision making part.
Replace the submit button from something like
<input type="submit" ...>
to
<input type="button" onclick="doSubmit(this.form);" ...>
and use something like
function doSubmit(frm) {
//whatever
frm.action=<your page address here>
frm.submit()
}
Is there a way to check the data sent by a form to a PHP page return to the form page WITHOUT resetting the data sent and show a error?
The form has 20 fields and I need to check one of them on a bd. If it fails the user may be redirected to the form page with the form populated and displaying a error message on the field which is 'wrong'.
I would like any advice of a technique instead of populating each field using PHP.
UPDATE:
I do not want to use any solution that involves repopulate the fields by myself!!!
I want a solution that return to the form page populated with the previous values. I've tried something like js.history.back or window.back(). But the form returns empty...
UPDATE: If you are looking for this type of behavior, nowadays there are several different techniques to achive this. I'm currently using jQuery (Ajax).
In your form fields HTML, add posted values as field value.
e.g:
<input type='text' name='email' value='<?php echo $_POST['email']; ?>' />
Yeah, just check the forms beforehand, make sure you run the form HTML after the validation procedure, and if validation fails, reload the form with all of the prior information in the fields.
It might look something like this:
<?php if(isset($_POST['submit']){
// validation code goes here with a trigger variable to
// contain true or false depending on the outcome
}
if($trigger==false){
// load up your post data here
}
echo '
<!-- form HTML goes here -->
';
On your Form Validation page
<?php
$form_values = $_POST;
//Your Form Validation
//$form_validated = TRUE/FALSE; // FALSE if there were errors
if ($form_validated) {$form_values = array();}
$_SESSION['form_values'] = $form_values; /* if your form validation page and form page is not on the same page and can also use the same technique for error messages */
?>
On your Form page for each input field.
<?php
$form_values = $_SESSION['form_values']; //if your form validation page and form page is not on the same page
unset($_SESSION['form_values']);
echo '<input type="text" name="user_name" value="'.$form_values['user_name'].'" />';
?>
Different input types will need to handle the $form_values array differently but this should get your started.
If you are not using a seperate form validation page, you can get the form values directly form the $_POST array.