php input send to URL, malformed - php

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.

Related

Codeigniter form action issue [duplicate]

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.

Drop-down list, click direct to new URL, no submit button, posting hidden PHP variables

I want to make a drop-down list, each selection being a new URL link, where there is no Submit button. Instead the user goes straight to the URL when they select from the drop-down. I can make this work, using the following HTML:
<select name="mydropdown" class="class1" onChange="document.location = this.value" value="GO">
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
However, I also want to post two hidden PHP variables to the selected page, $username and $password.
Any help much appreciated.
You can try this by submitting the form by jquery. Also pass the password in encoded format. Check below code ==>
function callredirect(pagename){
$("#frmdropdown").attr("action",pagename);
$("#frmdropdown").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" id="frmdropdown">
<input type="hidden" name="username" value="abc" />
<input type="hidden" name="password" value="password" />
<select name="mydropdown" class="class1" onChange="callredirect(this.value)" >
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
</form>
The best way to pass variables to other page is using the $_SESSION, since you don't have form and user doesn't want to know what variables/ values you are passing. read the docs here about PHP Sessions
NOTE: Advise not to pass password through out the page, it make your
system vulnerable for attackers. Best practice is verify user in each
page if you want or encrypt and store the password.

HTML update input textarea everytime "Submit" it clicked

<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example

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