Keeping Current URL after form submit PHP - php

Say I have e.g: (foo.com/index.php?a=1) and a form on it to submit data, but when the submit is hit the url refreshes and it gets rid of the values from the attached.
How do I make the url on my page to stay same as before even when the sumbit button is clicked.
Thanks

Like this: action="yourpage.php?var1=value1&var2=value2". You can get these with PHP like so: $var1 = $_GET['var1'].
Example:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
You can do this to add existing variables into the url:
<form method="post" action="yourpage.php?var1=<?php echo $_POST['var1'];?>&var2=<?php echo $_POST['var2'];?>">
[...]
</form>
<!-- Output:
<form method="post" action="yourpage.php?var1=value1&var2=value2">
[...]
</form>
-->

Change the form action in the HTML to post. i.e.:
<form action="..." method="post">
Assuming its a get at the moment. Then set action to your URL

Related

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.

form in subpage doesn't work

I have my page as follows:
<form id="filterQuery" action="#" method="post">
...
<input type="submit">
</form>
<?php
// use $_POST to determine query restrictions and conditions
?>
Now my problem is with the action of the form, because if I make it "#" self, nothing happens when clicking submit.
I'm using a template which shows subpages as include in <div>s. In other words, my page is not a standard HTML page which contain <head> and <body> etc, but is in the following format: basic.php#!/my_sub_page.
Trying to make action "basic.php#!/my_sub_page" results in an empty _POST
I tried your example and everything works fine with /index.php#!/smth in action and post variables.
<form action="#!/something" method="post">
<input type="text" name="var1" value="123">
<input type="submit"></form>
<?php
print_r($_POST);
?>
This is how I check it.
Are you sure you didn't forget name attributes in inputs?
Do you see their values in url after ? if you change method to get?

PHP $_POST form with $_GET after submit

I use PHP.
I have a form and after submit I want it to go to a URL with a $_POST variable at the end (as a $_GET), like this:
http://example.com/page.php?my_key=my_value
The problem is that "my_value" is created witin the form which means it does not know about it before the form is posted. Any ideas?
<form method="post" action="/page.php?my_key=">
<input type="text" value="my_value" name="my_key">
<input type="submit" name="submitter">
</form>
After sending form in PHP you can simple make 302 redirection to url you want.
In PHP file page.php (or in other file that is front controller) you can simple do:
if (isset($_POST['my_key'])) {
header('Location: http://'.$_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI'].$_POST['my_key'],true,302);
}
Use Javascript to change the action attribute.
I.E. Using jQuery:
jQuery('form').submit(function(eve) {
var action = eve.target.attr('action');
action = action + eve.target.find('input[name="my_key"]').val();
eve.target.attr('action',action);
});
A little rough, needs to be checked and maybe debugged.
You can Not pass an POST value through an url :
what you can do is something like this:
<form action="/page.php?my_key=" name="pre" method="POST">
<input type="hidden" name="my_key" value="my_value">
</form>
<script type="text/javascript">
setTimeout("document.forms['pre'].submit();",0);
</script>
now this will act as an link and will auto submit form as an POST.

Issues in working of PHP form submit with GET data

I am working with PHP forms.
The opening form tag looks like this:
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
This generated the following HTML:
<form name="adv_search_form" action="index.php?&rel=common_listing&module=company&sort_option=&search_option=&show_max_row=15" method="GET">
When I submit this form, I land on a URL:
http://localhost/projectcode12may2014/ampanel/index.php?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
But what I want is that
?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
should be appended to the action URL on the landing page. Can anyone suggest any good method for doing this?
Thanks in advance.
If you are using method="get" the parameters in action are overwritten. Only what is in the from will be transmitted.
You need to add hidden fields for all these values. For example:
<input type="hidden" name="module" value="company" />
That is your from data.
You have method="get" in your form tag. This means that the form data is concatinated to the URL. Use method="post" to have send form data as a POST request and keep your URI clean.
So change this line
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
to this
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="post">
Then you can access your form data in PHP with the global $_POST var which holds an array with the submitted data.
This is a normal behavior. Using GET method discard all parameters specified directly in action attribute.
Use POST method instead or move parameters from action attribute of the form to input fields with type="hidden".

Should Javascript Input to HTML form onSubmit with # action to PHP $_GET (all code on same file) work in theory?

I have the code request a date to be chosen by the user through this code:
<script type="text/javascript" charset="utf-8">
var k3 = new Kalendae(document.getElementById("cal"), {
months:3,
format:'DD-MM-YYYY',
mode:'single'
});
</script>
The user selects a date through the javascript and hits an html submit button:
<form name="input_data" action="#" method="post"
onSubmit="javascript:location.href="#?date_value=" + k3.getSelected();">
This code's html action directs it to reload the same page with # so that php can capture it with:
$dateValue= $_GET['date_value'];
If I echo $dateValue should it echo the original javascript input in theory? If no, how would it need to be modified?
Simply using
<form action="" method="post">
should work.
Say you call your button "Save" (name="save"). This will make trigger the PHP on submit.
if (isset($_POST['save'])) { // Your stuff here
You should instead update your calandar information on change, storing it in a hidden input-field. I know submitting a form with a hash-tag is causing problems in several browsers.
If you remove the # from the form tag like so:
<form name="input_data" action="#" method="post"
onSubmit="javascript:location.href="?date_value=" + k3.getSelected();">
The url will be appended ?date_value=Whatever and called, but the form submits and grab the action and post the data to it and if you keep it the page won't be called, so theoretically no cause the get would be lost because of the second page load with not get value, but you can get this working by using this editing to your form like so:
<script type="text/javascript">
function edit() {
document.input_data.action = "?date_value="+k3.getSelected();
}
</script>
<body>
<form name="input_data" action="" method="post" onSubmit="javascript:edit();">
...
</form>
</body>
And this will send the get value alongside the post values from the form and you can do it without the need to another function by updating the onSubmit attribute in the form and delete the edit function like so:
<body>
<form name="input_data" action="" method="post" onSubmit="javascript:this.action='?date_value'+k3.getSelected();">
...
</form>
</body>

Categories