Getting correct URL when using GET - php

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" />.

Related

Passing variable in a URL not working

So as usually I put in information in the url leading towards another page like this:
<form action="index.php?type=question">
<input class="log-btn extra" type="submit" value="Home">
</form>
Usually it always works, I even copy pasted it because I've used the exact same url before and on the other page it works but doesn't on this one it just returns empty after the questionmark like this:
index.php?
When you want to misuse a Button as a link with URL query parameters you can do it in two ways.
In your example you try to set query parameters in the action attribute of the form. That is working as long as the method is not get, because get-parameters and URL query is exactly the same thing and submitting the form will overwrite the query with the get parameters of the form elements.
<form action="home.php?type=question" method="post">
<input class="log-btn extra" type="submit" value="Home">
</form>
You can also set <form method="get"> and use a button:
<form action="home.php" method="get">
<button type="submit" name="type" value="question">Home</button>
</form>

html form removing PHP tags

I don't know if this is a duplicate, don't know. But, please let me explain. When I try to do this:
<form action="page.php?action=create_page" method="get">
It redirects to this url:page.php?title=something&content=something
The title & content is way it is intended, but it seems to remove action=create_page
It is probably a simple fix, but help me out anyway.
Try the hiddenfield
<input type="hidden" name="action" value="create_page" />
you can't directly pass get variables to a php page from an html form ..pass a hidden input value
example
<form action="page.php" method="get">
<input type="hidden" name="action" value="create_page"/>
</form>

Problems with FORM URL when switching to GET-method

I have a problem with creating a simple search form, used to search my database.
I started using the POST-method and everything worked fine, but switched to using GET-method instead to be able to bookmark my search results. But now I have trouble with the "action-url" when posting.
<form action="index.php?page=searchresult'" method="post">
This worked fine, I got my var's sent to the stated URL, but
<form action="index.php?page=searchresult" method="get">
Doesn't work. When I check my HTML code everything looks great, but I end up going to the URL
site.com/index.php?ALL THE GET-variables linedup here.
I am loosing the "?page=searchresult" part when using the GET-method?!
Am I missing something here, please help?!
Humble regards :)
Try changing this to
<form action="index.php?page=searchresult" method="get">
Put the page element as a hidden tag.
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult"/>
<!-- Rest of the input elements -->
<input type="submit" />
</form>
This should fix your issue.
If you are using GET methood then you can pass page parameter in hidden field
like
<input type="hidden" name="page" value="searchresult">
GET sends data in the query string. If the URL in the action has a query string, it will be overwritten by the new one generated by the form.
Store your data in hidden inputs instead.
Well, this is interesting, but you can fix it using an hidden <input>, such as this:
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult" />
<input type="submit" value="submit" />
</form>
Just compile the "value" of the hidden input with what you need to pass.
Tried it and it actually works.
Edit:
obviously, from php, use this:
<?php $page = ((isset($_GET['page'])?($_GET['page']):("nope")); ?>
Add a hidden page data in the form.
<form action="index.php" method="get">
<input type="hidden" name="page" value="searchresult" />
Simply because "page=searchresult" is a get data.
You have a quote too much, try removing it.
|
\|/
<form action="index.php?page=searchresult'" method="post">

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

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.

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