Submit button redirecting to different address - php

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

Related

Navigate to form after submission

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)

are form actions affected if you add the pound symbol to the url?

Can I add the pound symbol to form actions. For example, lets say that my form submits to forumsumbit.com, would it affect anything if I make it submit to forumsubmit.com#PostID2
<form action="<?= $_SERVER['PHP_SELF'] ?>?pageAction=POSTS" method="post" name="forum" id="addpost" enctype="multipart/form-data">
So that this, becomes this.
<form action="<?= $_SERVER['PHP_SELF'] ?>?pageAction=POSTS#PostID2" method="post" name="forum" id="addpost" enctype="multipart/form-data">
I want to do this, because I want the page to scroll to that section oft he page after it reloads.
Also, using the pound sign in urls have a specific name?
No. The part following the pound sign (called the "hash") is not processed by the server (unless you deliberately do something very strange to your server configuration). Having a hash in the URL won't affect anything about form processing, but it will cause the browser to jump to the named part of the page when the action page loads.

PHP Formatting URL within a Form Action

I would like to add the ID of a User to the form action as a URL.
For Example view_member.php?id=2
Can somebody possible show me the correct way to format PHP code in the form action below where I can include the id as $recptid
$recptid = $_GET['id'];
I have tried
<form action="<?phpview_member.php?id=$recptid?>" method="post">
I would like to be able to add a link but my formatting is wrong
// insert php code as URL in the form action the a submit is pressed
<form action="<?php?>" method="post">
</form>
Thanks in advance for any help
try this,
<form action="<?php echo "view_member.php?id=".$recptid; ?>" method="post">
i hope it will be helpful.

File extension added after form submit

Let's say I have a form on my website homepage: www.mysite.com
Now, the form tag looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
...
</form>
When that form submits, it does everything it's suppose to. But it also reloads the page with the full filename and extension. So the user will now find the URL in the address bar is: www.mysite/index.php
Is there a way to make the form fire to the same page without adding this extension?
There may be situations where the form is as an include in a footer, so I cann't be specific about the page the form needs to fire to, hense the PHP_SELF code.
That's because $_SERVER['PHP_SELF'] refers to the actual filename of the current script.
Try this:
<form action="" method="post">
An empty action will post back to the current URL.
Try setting the action to #:
<form action="#" method="post">
...
</form>
The # refers to the current page.
Try changing action to #, this post to the current page.
Edit: Mike beat me to it.
Edit 2: It looks like you can leave out the action all together and it will default to the same page.
Edit 3: Mike beat me to that one too.

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.

Categories