Add query string to URL if there is none already - php

I have a question. My question title doesn't really describe what I need help with, but it's the same concept. This is what I want to do:
I am creating a search function (not open for other suggestions than the one I am trying to accomplish), and it's using query string. If I search for "Hello", I want my website to redirect to website.com?s=hello, but if there already is one, add it to the url with a &, like this: website.com?page=home&s=hello. I tried to use this:
if(isset($_GET)) { // Add the string with & before
else { // Add the string with a ? before
I am willing to use jQuery if I have to
More brief explanation:
I have a form, it has an input and a submit-button.
<form action="" method="post">
<li><input type="text" placeholder="Username"></li>
<li><input type="submit" value="Search"></li>
</form>
I am aware how to store everything in variables and such, but I am not sure how to add it as a query string to the url. Thank you!

A way to do it is to handle it in php in your form:
<?php
if(isset($_GET['page'])) {
$url = '?page=' . $_GET['page'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>`
Note that if you want to submit your form AND go to the page ?s=foo as a result of submission, you must have a method GET on your form and have an input with argument name="s".
EDIT: Or a bit more exhaustive, if you have several arg that could be passed in the URL it could be :
<?php
if(!empty($_GET)) {
$url = '?' . $_SERVER['QUERY_STRING'];
}else {
$url = '';
}
?>
<form method="GET" action="<?= $url ?>" >
Search <input type="text" name="s" />
<input type="submit" value="Search" />
</form>

Related

Pass unencoded url from form

I've set up a search box on the homepage, that should link into the Opencart search by passing a URL.
The problem I'm having is that the resulting URL is always unencoded:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
What I'm aiming for is
store/index.php?route=product/search&filter_name=test
My current code is:
<?php
$storesearch = $_REQUEST['store-search'] ;
$filter = 'route=product/search&filter_name';
$filtered = html_entity_decode($filter);
?>
<form id="store" method="GET" action="http://localhost/vinmobile/store/index.php?<?php echo $storesearch; ?>">
<input type="text" id="store-search" name="<?php echo $filtered; ?>" value="Store Search" onclick="this.value = '';">
<input type="submit" name="" value="Search">
</form>
<?php echo $filtered; ?> // Just to show it onscreen to make sure it's written correctly
I've tried encode, decode, html_entity_decode, urldecode, rawurldecode...
I've tried the form method as GET, POST
I've tried the variable declaration as $_REQUEST, $_GET, $_POST
Every time, I still end up with the url looking like:
store/index.php?route%3Dproduct%2Fsearch%26filter_name=test
I'm sure it's something simple I've missed, but I 'think' I've tried everything and would be very grateful if someone could point me in the right direction.
Cheers
webecho
Ended up using Javascript instead and it works perfectly:
`<script>
function process()
{
var url="store/index.php?route=product/search&filter_name=" + document.getElementById("filter").value;
location.href=url;
return false;
}
</script>
<form id="store" onSubmit="return process();">
<input type="text" name="filter" id="filter" value="Store Search" onclick="this.value = '';">
<input type="submit" value="Search">
</form>`
Thanks for your suggestions though

How can i output a string in a label on page?

I am trying want to ouput a string ,stored in a variabel, in a label on a page when i click on a button.
But i can't find out how. Still a beginner.
<form action="Test.php" method="post">
Output text: <input type="label" name="word" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
if (isset($_POST['submit']))
{
//something that gives the label value $word//
}
?>
There are a few things wrong with your code.
Let me outline them.
Your submit input should have a name attribute, since your conditional statement is based on it if (isset($_POST['submit'])){...}, something I've modified to check if the input is not left empty, using PHP's empty() function.
The input type you have for your "Output text" is invalid, it should be type="text" and not type="label", there is no type="label".
method="submit" for your submit button is invalid for a few reasons. Method belongs in <form> and there is no method="submit".
You then need to assign a POST variable from the input:
such as:
$word = $_POST['word'];
Plus, from what looks to me that you're executing the entire code from within the same page, you can just do action="", unless your code is set in 2 seperate files.
In regards to what you want to achieve: You can then echo the input (if one was entered) using a ternary operator and giving it (the input) a value.
I.e.:
value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>"
Here:
<form action="" method="post">
Output text: <input type="text" name="word" value="<?php echo isset($_POST['word']) ? $_POST['word']: '' ?>" />
<input type="submit" name="submit" value="Print!" />
</form>
<?php
if ( isset($_POST['submit']) && !empty($_POST['word']) )
{
$word = $_POST['word'];
echo $word;
}
?>
If you want to use a "label" for your input, then use:
<label for="word">Output text:
<input type="text" name="word" />
</label>
You should also guard against XSS attacks (Cross-side scripting) using:
http://php.net/strip_tags
http://php.net/htmlentities
http://php.net/manual/en/function.htmlspecialchars.php
I.e.:
$word = strip_tags($_POST['word']);
$word = htmlentities($_POST['word']);
$word = htmlspecialchars($_POST['word']);
A few articles you can read on XSS:
http://en.wikipedia.org/wiki/Cross-site_scripting
https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
Your code should look like this
<form action="Test.php" method="post">
Output text: <input type="label" name="word" value ="<?php echo isset($label['data'])?$label['data']: '' ?>" />
<input type="submit" method="submit" value="Print!" />
</form>
<?php
$word = "test";
$label = array();
if (isset($_POST['submit']))
{
//something that gives the label value $word//
$label['data'] = $word;
}
?>
This should work
Regards
Ahmad rabbani
First of all your input element has type = label, it doesn't mean anything. Change it to type=text
And you are submitting value but not printing it. So in input field you have to print it also.
Look below code.
<?php
$word = "test";
if (isset($_POST['submit']))
{
// whatever you do with $word
}
?>
<form action="Test.php" method="post">
Output text: <input type="text" name="word" value="<?php echo $word; ?>"/>
<input type="submit" name="submit" value="Print!" />
</form>
UPDATE
One more thing I forgot to mention that you are submitting form to Test.php and printing this to file, if this file's name is Test.php then not an issue, other wise leave action property blank, so it submit data to itself.
method = submit there is nothing like this. you can set button name to submit, like name= "submit".

Trouble getting echo syntax correct for html code

I am returning text results from a PHP FOREACH loop, and I want to add the links add and delete for each result where it looks like this: ADD | DELETE. The trick is, I need each link to be a form that is styled as a link so it looks like text links but I can use Post when the links are clicked. I can style the links with the class name "formlink", but my question is, how will the statement syntax look? I keep getting T_variable errors. Can someone help show how the code below so it will display error-free on the page?
<?php
echo "<hr>" . $results[text]
<form action="" method="post"><button type="submit" class="formlink">Approve</button>
</form> | <form action="" method="post"><button type="submit"
class="formlink">Delete</button></form>
That's one messy code though. Well I dunno, I could give you a brief idea, and the rest is up to you.
<?php
// Check clicked button
if (isset($_POST['btn_approve'])) {
$username = $_POST['username'];
}
if (isset($_POST['btn_disapprove'])) {
// Do something
}
?>
<form action="" method="post">
<input type="text" name="username" value="<?php echo (isset($username) ? $username : ''; ?>">
<input type="submit" name="btn_approve" class="form-link" value="Approve">
<input type="submit" name="btn_disapprove" class="form-link" value="Disapprove">
</form>
You could check isset() and How to write ternary operator.
<?php
echo "<hr>" . $results[text];
?>
<form action="" method="post">
<button type="submit" class="formlink">Approve</button>
</form> |
<form action="" method="post">
<button type="submit" class="formlink">Delete</button>
</form>

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Mod Rewrite and search query string

To search something, user need to use following URL:
http://.../results/query/something
I got my text input, in which user type the string to be posted, but it looks then following:
http://.../results/query/?query=something
I've tried to change the 'post method' type, but doesn't bring any good results. How can I do that, so it'll look like that?
http://.../results/query/something
<form id="myform" method="post" action="/">
<input type="text" id="query" name="query" />
<input type="submit" name="submit" onclick="return go();" />
</form>
<script type="text/javascript">
function go(){
var query = document.getElementById('query').value;
document.getElementById('myform').action = 'http://yourdomain/results/query/'+query;
}
</script>
Simple answer:
Use method="post"
Construct the URL with PHP after the form is posted, using urlencode() and any other appropriate sanitization on the value.
Redirect to the page you want.
$query = isset($_POST['query']) ? urlencode($_POST['query']) : '';
header("Location: results/query/$query");

Categories