Navigate to form after submission - php

This might seem a simple problem, but im stuck on this one. Hope anyone can help me on this
I'm working on server side form validation in PHP. Everything is working as expected as far as validation goes. But if an error is shown on input or the form gets submitted the browser navigates to the top of the page. How can I prevent this behaviour? I need the page where it is after I click the submit button
<?php include('process_form.php'); ?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="form-row">
<div class="col form-group">
<label>Primeiro nome</label>
<input type="text" class="form-control" title = "Inserir nome" name="firstname" value="<?php echo $firstname ?>">
<span class="error"><?php echo $firstnameErr ?></span>

If I understand your question, you are asking quite a lot from just HTML and PHP. Remember that once the form is submitted, the browser navigates away from the current page and loads the form action page (in your case, it reloads the current page as per the directive action="<?php $_SERVER['PHP_SELF']; ?>".
So, how would you position the page at exactly the desired location if there was no form submission going on? That is how you would do it in this case. So, as suggested in the comments, you could modify your action directive: action="<?php $_SERVER['PHP_SELF']; ?>#id_of_form_container". For example, if your form is in a div structure like this:
<div id="contact_form_div">
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
then your action tag would be:
action="<?php $_SERVER['PHP_SELF']; ?>#contact_form_div"
Alternately, you can do some basic form validity testing on the javascript side, during the form submit process. If, for example, a required field is blank, you can return false; - which will stop the submit process and return control to the user.
Here is an example of what basic javascript field validation looks like. And here is an example of using javascript/jQuery to interrupt the form submit process to perform that validation, and return control to the user (via return false;) if validation fails.
References:
MDN article on form validation - note the SmashingMagazine links at bottom
TutorialsPoint - more concise example of the same
Video tutorial of same (30 min)

Related

How do I preserve parameters in PHP form code

I have a script which is run with a parameter (e.g. details.php?studentid=10325).
On this script I have a form with the following form code so that the form data is sent to the current script. However, what's happening is that the script is running without the parameter. How do I preserve the parameter in this form code?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Not really recommended, but you could add a hidden <input> tag with the studentid so it is sent back when you submit the form. Like so :
<input type="hidden" name="studentid" value="<?= $_GET['studentid'] ?>">
Leave the action blank, and the form will submit back to the current url
<form method="post" action="">
Keep in mind you have specified post method, so the form values will come thru in $_POST, whereas your studentid on the query string will be in $_GET['studentid'], you can work around that by using $_REQUEST['studentid'] instead, but make sure you don't have a field in the form also called studentid

Submit button redirecting to different address

So the submit button of this form is supposed to send the data to localhost/mvc/contact/test and redirect to localhost/mvc/contact
at the first click of submit button it works well
Right
but when I click the submit button again its taking me to localhost/mvc/test
Not right
here is the html code of the form <form action="test" method="post" accept-charset="utf-8" class="contact">
when I had this as <form action="contact/test" method="post" accept-charset="utf-8" class="contact"> it first redirected me to localhost/mvc/contact but on second attempt redirected to localhost/mvc/contact/contact/test
Btw it's a simple mvc framework you may check it out on github for more clarity on Leggera # Github
The above mentioned code is present on view/contact/index.php
You can try these things:
You must give the full path to the file like <form action="contact/test/index.html" method="post" accept-charset="utf-8" class="contact">
In the index file of contact/test you can use <?php header("Location: http://www.example.com/");?> for redirection.
Maybe you have given wrong paths to files.
You must give more details for more specific answer

html form action="." not submitting to self, but instead submitting to root directory

I have a form in one of my pages and I want to make it submit to the same page. I am doing this:
<form method="POST" action=".">
But when I submit the form it submits to the root directory of my site instead of to the same page the form is on.
I know that the form is being submitted because I tried temporarily changing the method to GET and when I submitted the form, the URL showed the get variables.
Why is the form not submitting to the current page?
If it's relevant, I'm using PHP with xampp.
Try this:
<form method="POST" action="">
This is what I do
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
I'm a newbie though so if Juampi's answer works too , I'd go with it.

Using PHP, how do you show text entered from a form to the bottom of the same PHP page?

Using PHP, how do you show text entered from a form to the bottom of the same PHP page?
I am trying to show user input at the bottom once submit is pressed
<form name="assignment2" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
It sounds like you want AJAX. To do that, the form should post an XMLHttpRequest to the server; JavaScript on the page should then dynamically modify the page DOM to display the text you want.
If you're okay with it being static, just have the PHP page display just the form if there's no input data, and the data if there is input data.
You don't need AJAX or jQuery; you can keep this to simple PHP:
<?php
echo '<form name="assignment2" method="post" action="'. $_SERVER['PHP_SELF'] .'">';
// Other form elements
echo '</form>';
if(isset($_POST['submit']))
{
echo '<input type="text" value="'. $_POST['value_you_want'] .'">';
}
?>
As Basti has pointed out, you should always be cautious about the possibility of cross-site scripting so you should only really use this code above with non-sensitive data or data which will go nowhere near a database.
Since your form is sent via POST request, all of the form's inputs can be accessed using $_POST. You can simply put them into your inputs as value.
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="inputname"
value="<?php echo #htmlspecialchars($_POST["inputname"]); ?>" />
</form>

Search MySQL with PHP and display results on the same page

This is definately a novice question, but if you could be of any help i would be very grateful.
Basically, i'm building a database management page, and it of course includes a search function.
So, the search form looks something like this
<form name="name" function="search.php" method="get">
But, whenever i use it, i will of course get redirected to search.php. What i want is a way to display the results on the same page i did the search from (let's say index.php), without having to build an entire identical page around search.php
Thankful for answers.
Use a hidden field in the form that indicates that the form has been submitted.
In your form page (e.g. index.php)
<form name="name" action="index.php" method="post">
{OTHER_FORM_FIELDS}
<input type="hidden" name="doSearch" value="1">
</form>
So in your php code (could be in the index.php page or in a php script included)
<?php
if($_POST['doSearch']==1) {
//query database
//get results
} ?>
in your index.php page
<?php if($_POST['doSearch']) { //a search request was made display my search results ?>
HTML_CODE
<?php } ?>
Let the page submit to itself:
<form name="name" function="index.php" method="get">
In the handler for the page, check whether or not you have parameters and display either the input box or the results as appropriate.
You could even take it one step futher. You could use AJAX to insert the results directly into the page content when the submit button is pressed, rather than causing a page refresh.

Categories