Post-variables only passed, when action="" is empty - php

In a php-script i have a form, method is post, action-attribute is empty, which is working so far. but when i add a value into the action-atribute, like so:
action="index.php?id=9&get-id=5"
the whole post-array is empty after submitting.
Someone has any idea what this could be about?
Thanx in advance, Jayden
edit: here is an Example:
$form = '<form name="form1" method="post" enctype="multipart/form-data" action="index.php?id=9&get-id=5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>';
The form is displayed in a tab in a js-tabmenu, which opens also by get-parameters, in each tab is a form and after submitting the get-param is needed to display the right tab with the right form.

try to use $_REQUEST
which is collection of $_GET and $_POST

You should not use both GET and POST in a request.
You must only use post, therefore the two variables 'id' and 'get-id' should be in the form (use hidden fields)
edit:
try changing your code to:
<form name="form1" method="post" enctype="multipart/form-data"
action="index.php?id=9&get-id=5">
<input type="hidden" name="id" value="9">
<input type="hidden" name="get-id" value="5">
<input type="text" name="name1" value="">
<input name="submit" type="submit" value="submit">
</form>
then if you :
print_r($_POST);
at the top of the index.php page you should be able to see what is going on.
Also - just to check are there any redirects in your code, ie does index.php then redirect somewhere else as that would cause the $_POST to get lost

If you're trying to access id or get-id from your script: Those were appended to the url, even if you submit that form via post. Therefore you will find their values in $_GET, as usual. Only the values of <input> fields (and textarea etc., simply: all form elements) are in $_POST.

Related

Send post form to the same page using php or html only

I can only use php and html to do this.
I'm trying to pass the input of a text box to the URL. Something like this ?input=
The page will be the same.
<form method='post' action="">
<input type="text" name="input">
</form>
If you want the values to be in the url you need to change the method of your form to get
<form method="get" action="">
This will cause your url to look something like http://mydomain.com/index.php?input=something. All values passed in the URL like this are then accessible via $_GET.
<?PHP
echo $_GET['input'];
?>
You don't need the anchor tag as you have it. Just changing the form will cause the form to submit as you want it.
<form method='get' action="">
<input type="text" name="input">
<input type="submit" value="Submit">
</form>
if you wanted to append all form input in the url then you should use get form method.
and you should not use anchor tag because it seems that it is meaningless.
So your form should be like this:
<form method='get' action="">
<input type="text" name="input">
<input type="submit" value="Submit">
</form>
After submitting the form you will url like this:
http://yousite.com/this_page?input=typed_value
you can access your purl params by using global $_GET or $_REQUEST (e.g $_REQUEST['input'])

$_POST to a form on another page

I was originally using a $_GET method:
''
Using this works, but i've been told that using a $_POST method is better, less messier and just the preferred choice.
So i'm wondering how i would implement the very same thing as above, but using a $_POST method.
You can submit a form with method post or use an ajax request. In jquery this would look like:
$("#myLinkId#").on('click',function() {
$.ajax({url:'phpFile.php',type:'post',data:{...you data here...}});
});
You need to turn this into a form or use jQuery to trigger an Ajax call on click. Here's a form example:
<form action="editNews.php" method="post">
<input type="hidden" name="starts" value="<php echo $starts?>">
<input type="hidden" name="end" value="<php echo $end?>">
<input type="hidden" name="event" value="<php echo $event?>">
<input type="submit" name="submit" value="Your Link" data-icon="edit">
</form>
To send data via PHP to another page/form, you can do this:
Original form:
<form method='post' action='new-page.php'>
inputs here, etc.
submit button here
</form>
So this will, upon clicking submit, send you to new-page.php with all of the form data held in the PHP variable $_POST which is an array.
You can access the data by referencing the array with the name of the input, for example, say that you had:
<input type='text' name='NAME' />
You can reference this data on new-page.php with $_POST['NAME'].
I hope this helps you!

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

Getting correct URL when using GET

When clicking a button, I would like my URL to become this:
users.php?action=search&formvar1=value&formvar2=value&...
This is what I've tried:
<form id="search" action="users.php?action=search" method="get">
But this doesn't seem to work (it doesn't add the action=search part). Is there any way to do this? I know it works when using POST instead of GET, so why wouldn't it here?
Thank you
You can do that similarly to a POST form. Simply include the default attribute as hidden form field:
<form id="search" action="users.php" method="get">
<input type="hidden" name="action" value="search">
This way it will be added as parameter to the URL like all other variables.
The browser is building the query string from scratch.
Instead, you can add an <input type="hidden" name="action" value="search" />.

Javascript/HTML Form field naming issue

Consider the following simple form:
<form method="GET" action="handle.php">
<input type="hidden" name="action" value="search">
</form>
Form submission is performed by Javascript (iui) in an ajax call. All fields are properly gathered from the form. Javascript then wants to send the ajax call to "form.action".
This is where my problem starts. The object form is of type HTMLFormElement. The action property of the form is supposed to be of type string and should contain "handle.php". After some hours of debugging, I noticed that form.action is now of type HTMLInputElement.
My question:
Is this proper Javascript behavior? I would have never though that defining a form field with the name of a form attribute, this would happen. In the mean time I solved the issue by naming my field differently.
Thanks in advance for any advice...
Found an easy way of displaying my problem. First the form with the problem:
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
And the form that is proper:
<form action="test.php">
<input type="hidden" name="NOT_AN_ATTRIBUTE_NAME" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
In the first, the popup states "[object HTMLInputElement]", in the second: "http://localhost/test.php".
The issue you're seeing is because forms are special in JavaScript. All their fields are accessable as properties, so when you use this.form.action, it's getting the field action, not the HTML attribute action="test.php".
Try changing alert(this.form.action); to alert(this.form.getAttribute('action')) instead.
It seems as bug. Maybe there should be an array for 'action '
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action[0]);"> //the form action
<input type="button" onclick="alert(this.form.action[1]);"> // the text input
</form>
your this.form.action is still a object. instead of using alert put it in a console.log(this.form.action) and use firebug and firequery try to discover what events/properties your this.form.action has.
!you need to enable your console in firebug before you can use console.log

Categories