Codeigniter form action issue [duplicate] - php

So I'm trying to submit a page to itself while retaining the current query string of the page.
So the page is sb.local/sb/cat.php?brandcode=JM&t=cat_items I pull off the query string and stick it back into the html form to preserve the parameters. This is the resulting form:
<form id="brand-select" method="get" action="?brandcode=JM&t=cat_items" name="brand-select">
Brand:
<select id="brandcode" style="width:207px" tabindex="3" name="brandcode" required="">
<option value=""></option>
<option class="brand-option" value="AX" data-brandid="110"> Aetrex </option>
<option class="brand-option" value="AL" data-brandid="12"> Alden </option>
<option class="brand-option" value="ETC" data-brandid="11"> Etc </option>
</select>
<input type="submit" value="go">
</form>
When I submit the form by choosing the dropdown for Aetrex (value AX), however, it goes to a url of:
sb.local/sb/cat.php?brandcode=AX
in other words, it cuts out the "t=cat_items" that is in the action. It also cuts out the "brandcode=JM" but I would almost expect that since they're duplicates.
That's not what I expected, I expected that if there is a query string in the action attribute, it would append form values to that query string (e.g. sb.local/sb/cat.php?brandcode=JM&t=cat_items&brandcode=AX. Instead it seems to be replacing the query string entirely with only those elements that are in the form.
Is the form action attribute not usable for storing query parameters, only more basic url info?
Edit: Note that I can work around this by parsing every parameter and then putting each parameter into its own hidden field manually, except for any parameters that I want to allow to change, I was just hoping that there was some kind of simpler way.
I tested with a non-conflicting query string and that was replaced in whole even when there wasn't a conflict (in Firefox), so based on that it seems that query strings are useless in the action attribute of get forms? Or am I missing something.

I know this is an old question, but the solution is actually pretty simple (and neat!).
All you have to do is sending the querystring with hidden input fields in the format name="key" and value="value".
?brandcode=JM&t=cat_items would "translate" into:
<input type="hidden" name="brandcode" value="JM" />
<input type="hidden" name="t" value="cat_items" />
Completely remove the querystring from your action.

Change your code to:
<div>
<form action="?brandcode=&t=" method="get">
....
</form>

You can use "POST" method instead of "GET" method for form submission, if the method doesn't matter.

Related

php input send to URL, malformed

I want to send the text inside the input field to the URL and trigger the $GET function.
<form action="" method="get">
URL:<input type="text" name="url" size="100px" placeholder="URL"/>
<select name="url">
<option value="<?php filter_var($_GET['url'],FILTER_VALIDATE_URL); ?>">testdomain1.de</option>
<option value="test2">testdomain2</option>
<option value="test3">testdomain</option>
</select>
<input type="submit" value="Send"/>
</form>
It works but link is malformed:
I want this:
http://127.0.0.1/title/index.php?url=http://www.tech.de/news/google-will-kein-eigenes-auto-mehr-bauen-10092492.html
but I get this
http://127.0.0.1/title/index.php?url=http%3A%2F%2F127.0.0.1%2Ftitle%2Findex.php%3Furl%3Dhttp%3A%2F%2Fwww.tech.de%2Fnews%2Fgoogle-will-kein-eigenes-auto-mehr-bauen-10092492.html&url=%3Cbr+%2F%3E%0D%0A%3Cb%3ENotice%3C%2Fb%3E%3A++Use+of+undefined+constant+url+-+assumed+%27url%27+in+%3Cb%3EC%3A%5Cxampp%5Chtdocs%5Ctitle%5Cindex.php%3C%2Fb%3E+on+line+%3Cb%3E22%3C%2Fb%3E%3Cbr+%2F%3E%0D%0A
could you help me?
ok i read the comments, and changed to this:
<?php filter_var($_GET['url'],FILTER_VALIDATE_URL); ?>
but it doesn't working, there is no way to paste the plain text to the url?
There is the way to get plain text to url, but it's not secure, because you must not use sanitizing like filter_var or htmlspecialchars.
Also, $_GET['url'] is not set before submitting the form so first send will lead to errors.
Option here is to include javascript to set action in form on keyup and then, when you press submit, you will be redirected to this url. If this is not what you want, please, tell us what your script need to do to get the correct answer.

Why do I have to use POST instead of GET?

When I use <form action="code.php?id=1" method="post"></form>, the form id is passed in the URL. But when I write the same code by replacing 'POST' by 'GET', the id is not passed to the URL.
Why?
When you submit a GET form, the values from the form are appended to the action URL, as the "query string" after the ?. Specifying an existing query string in the action attribute of such a form creates an ambiguity. Browsers don't merge the two query strings, they just throw away the old query string and build the new one based on the form.
With a POST form, there is no ambiguity: the data from the form is sent separately from the URL, so there is no need for the query string to be over-written.
However, it's probably best not to mix the two kind of parameters, so the solution is always to include your extra parameters as hidden fields, then it will work with both GET and POST forms:
<input type="hidden" name="id" value="1">
Better way is to pass id in hidden field.
<form action="code.php" method="post">
<input type="hidden" value="1" name="id" />
</form>
If your form is as below
<form action="code.php?id=1" method="post">
<input typ"text" name="username" />
<input type="submit" />
</form>
example script in code.php
<?php
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
?>
You will get form data in post array and url parameters in get array, in request you will get both get and post data in one array. But if you change from post to get method your form data added with the url instead of appending. This issue is because of ambiguity. To get solution i this situation, create a hidden field in your form those you also want to send with query string.

url formatting when processing a form

So I have written this code for a form (search form) and it looks like this:
<form action="" method="get">
<select >
<option value="name">Client Name:</option>
<option value="email">Client Email:</option>
<option value="tnx_id">Transaction ID:</option>
<option value="amount">Amount:</option>
</select>
<input type="text" name="svalue" placeholder="Search"></input>
<input type="image" name="submit" src="img/search_btn.png"></input>
</form>
When user clicks Search I want the url to be formatted this way:
http://localhost/pp/index.php?search&name=haha
But instead I get:
http://localhost/pp/index.php?search=name&svalue=haha&submit.x=0&submit.y=0
I'm only having difficulties formating the url correctly because the rest of the
code is good, I cannot get rid of that &submit.x=0&submit.y=0 at the end of the
url too. Do you have any ideas guys?
Basically my desired url format gets the selected option (name, email, tnx_id, amount)
and assigns svalue's value to it.
I'd appreciated your help,
Thank you in advance,
Thanks.
That is how forms work. Each control gives a name=value pair in the submitted data.
You could hack it with JavaScript, but that becomes fragile. Give the <select> element a name and change the server side script so that it takes the two pieces of data as separate pieces of data.
I cannot get rid of that &submit.x=0&submit.y=0 at the end of the url too
Remove the name attribute from the image map so it won't be a successful control.
<input type="image" src="img/search_btn.png">
Other improvements you should make:
<input> is an empty element. It should not have an end tag. If you are writing XHTML then use the empty element syntax.
placeholder should not be used as a replacement for <label>
Add an alt attribute to the image map
Use JavaScript to create the URL. If you're using jQuery:
var url = 'http://localhost/pp/index.php?search&'
+ encodeURIComponent($('#id-of-your-select-element').val())
+ '='
+ encodeURIComponent($('#id-of-your-svalue-element').val());
Edit: As for getting rid of submit.x and submit.y, see #Quentin's answer.
in your php code you can add this:-
if(isset($_GET['name']))
{
$searchString="&cs=".$_GET['name'];
}
then use this:
http://localhost/pp/index.php?".$searchString."/
i think this will help you to get the right thing. if you want to use pagination with that,i have a code for you which i used before and it works great. just try it once!

Passing form values to controller action params in CakePHP

In my HTML form I need the URL output like this:
"example.com/mycontroller/action/mike/apple"
When I submit form with "post" my form values are not visible and can be get through "_POST". I don't prefer this because it makes output like this: "example.com/mycontroller/action/"
When I submit with "get" my form becomes "example.com/mycontroller/action?type=mike&key=apple"
I also don't prefer to change form action with javascript, like: "onSubmit take value of select and append it to form action" Because I don't want this snippet be javascript dependent.
I can submit form with "get" or "post" parse system variables of POST or GET and make a redirection. Like this:
Redirect this "example.com/mycontroller/action?type=mike&key=apple" to this "example.com/mycontroller/action/mike/apple"
But I didn't find this solution well designed.
Is it possible to pass form values with slashes (other than questiın marks)
< form class="well form-horizontal" id="myform"
method="get" action="/mycontroller/action" >
<select name="type" id="type">
<option value="mike" selected="selected">
mickey bricks</option>
<option value="albie">albert</option>
</select>
<input type="text" name="key" class="input-xlarge"
id="key" required="required">
<button type="submit" class="btn btn-primary" id="submit">
submit
</button>
</form>
If you make a POST form, it doesn't really matter where does it post to, what matters is where does it redirect. Many site searches redirect you to www.domain.com/search/search_terms so if this is a search form, there is nothing wrong with redirecting to ../action/mike/apple.
Additionally, if it is a POST form, it will not be filled in by the search crawlers (or at least not to my knowledge), so again it shouldn't matter where does the form post to, what matters is the return value and where does it redirect.
It all really depends on what are you trying to accomplish.

how to create a search form in php and mysql with pagination?

I am trying to create a search form with php and mysql and do a pagination
so i have a form that looks like this:
<form action="search_results.php" method="post">
<select name="cat" id="cat">
<option>Select Category</option>
<?php
foreach($categories as $category) {
echo "<option value=\"{$category -> name}\">{$category -> name}</option>";
}
?>
</select>
<select name="sub-cat" id="sub-cat">
<option>Select Sub Category</option>
</select>
<label>Price Range</label>
<input type="text" name="from" placeholder="From" />
<input type="text" name="to" placeholder="To" />
<input type="submit" value="" name="submit" />
</form>
my problem is that i get the first page in the search_result.php
and when i try to go to the second page the $_POST['submit'] wouldn't be set because it didn't get any submit
anybody has an idea how to solve this issue
Thanks in advance.
You should probably use GET in this case for the form and for the subsequent pagination links for the very reason that you specified.
POST is better used as a one off action where GET is a request for resources which can be filtered and hacked.
search_results.php?cat=1&sub-cat=2&page=2
search_results.php?cat=1&sub-cat=2&page=3
search_results.php?cat=1&sub-cat=2&page=4
search_results.php?cat=1&sub-cat=2&page=5
The easiest way would be to generate "next" and "previous" URLs that have the search information in the query string, and then have the receiving page read them from $_GET instead of $_POST. The $_GET array works exactly like the $_POST array, just for variables that were passed in the URL.
Another option would be to create a form where the search information is present in hidden fields, and use javascript to cause the "next" and "previous" links to change the value for the page to get, and then submit the form. This would allow you to use the exact same POST-based code you're using now, though it prevents users from bookmarking search pages and may have other unintended effects.

Categories