Autofill a textfield with url from another website through bookmarklet - php

I need a bit of clue here. What I want to do is to allow user to submit a url to my website through a bookmarklet. So when they click on the bookmarklet, the textfield with id (field_url) will be autofilled with the url on the page they are on. I looked around and found that I need to use this piece of code for the bookmarklet.
javascript:location.href="http://mywebsite.com/geturl.php?url="+encodeURIComponent(location.href);
My question: what should I put in geturl.php in order to autofill the textfield?

You're passing the URL through a GET parameter, so normally you'd just have to echo it in your php page:
<input type="text" id="field_url" value="<?php echo $_GET['url']; ?>" />
The shorthand method is <?=$_GET['url']?>, but I prefer to stay away from PHP shorthands.

Related

How do I rewrite URLs to make them shorter and not show any GET variables in the address bar

I have a link structure on single index.php page such as index.php?lang=eng&theme=dark&page=shop for example. I do need to pick up GET variables from the link to give appropriate content and configuration to each visited page. However I just want to show name of the page such as default.php or I if possible I want to show something like shop.php if the page=shop and chat.php if the page=chat. Can anyone recommend me what technique I should use at all?
If you want to do that, you should use POST rather than GET because it won't show its variables in the URL bar.
In PHP, you can find the value of POST variables by using $_POST['var'] rather than $_GET['var'] but how will you use open them in the first place if they won't show in a URL?
The answer is to use a form which appears to be a normal link, with elements inside it as variables for the PHP POST stuff. Here's an example:
...
<form name="openPage2" method="post" action="path/to/page2.php">
<input type="hidden" name="lang" value="en" />
<input type="submit" value="Page 2"/>
</form>
...
Now, what this will do is quite simple really. Say the current page containing this form is mywebsite.com/index.php. There's a button labelled Page 2 and everything else in the form is hidden. When you click page 2, the browser will go to mywebsite.com/path/to/page2.php without any visible variables in the URL bar. But, we've put hidden inputs into the form which will be submitted as POST when you click Page 2. Meaning, on Page 2, you can do this to get the language:
...
<?php
$lang = $_POST['lang'];
setLang($lang);
?>
...
and $lang will now be "en" because that's the form value. You can put as many hidden inputs as you like and none of them will show up in the URL bar because they are POST, not GET.
Hope that helped!

About PHP form , redirection

Suppose my Form codes look like this
URL : localhost/my-url.php
<form action="hello.php">
...bla bla bla
</form>
I will process the data in hello.php and i want to redirect to user to same url after processing (according to above example)
localhost/my-url.php
I know we can use header but i don't know how to get that url from which form was submited :(
Googled but didn't found any use full.
Thanks.
Add a hidden value in your form:
<input type="hidden" name="lastUrl" value="<?php echo $_SERVER['REQUEST_URI'] ?>" />
You now have the URL in $_POST['lastUrl'] data. You need to do it that complicated because $_SERVER["HTTP_REFERER"]; is send by the browser, and not all of them do this reliable.
You should put a hidden field in your form and set its value to current page url.
Then you submit the form and get the value of hidden field.
Then you can redirect user to hidden field (which is actually a URL of the page where you are submitting form) by using javascript or php.
You can use the
$_SERVER["HTTP_REFERER"];
to get the original URL where the form was posted from.
Remember to escape it, if you use it however. ]
Alternatively, you can process the form using AJAX, send process things (redirection) client-side.
Note that form data can be changed and intercepted if you wish to send the URL of the page as form data.

How can I pass a simple search query into the URL?

I have a search form on my site,
and I want to pass the text in the form to the URL,
like: mysite.com/search.php?q=apples (if search word was apples).
I figure that way people can bookmark their searches.
One solution I thought would be to catch the searchword in search.php and then reload into a new made URL. But it's not very elegant to reload like that. So how can I do it - I mean, how is it normally done? Do I need to use jQuery?
Clarification: I know how to get the vars from the URL in php. What I need is to control the URL that will be opened when the user presses SUBMIT, and the URL needs to contain the user's search word! Just like Google or DuckDuckGo, I put "apples" and the URL becomes ...?q=apples. But - how?! (Then I'll pick that up in the search.php, of course, but I know how to do that.) This is what I have now:
<div id="topnav">
<form action="search.php" method="post">
<input name="searchword" type="text">
<input type="submit">
</form>
Thank you so much.
Upon reading the clarification. What you need is a search form that submits to your search.php for example:
<form action="search.php" method="get">
<input type="text" value="search word" name="q" />
<input type="submit" value="submit" />
</form>
This will pass whatever value entered in the input named q to the search.php script.
If you post a HTML form which includes a text field with name 'q' and value 'apples' then the URL you want is automatically created by the browser. You definitely don't need JQuery for that.
how about using the POST-Redirect-GET pattern? [http://en.wikipedia.org/wiki/Post/Redirect/Get] also http://blog.andreloker.de/post/2008/06/Post-Redirect-Get.aspx
This would allow you to keep the url in the browser:
yoursite.com/search.php?q=apples
Alternatively, you can use javascript to set the location.hash of the url in the browser w/ the query information after the postback; I suspect this is actually what Google does.
eg,
yoursite.com/search.php#apples
So the form action would be search.php, the field would be called q and the method would be Get?
You should be able to handle all this from the html form if I'm understanding what it is you're trying to achieve.
if you have a form then must have declared form methoed POST/GET
in you search.php you can simply do this $_POST['name of the input field'] to get the word string,
and if you want to pass variable in url then you need to make a link through Link

How to get url value in php?

I wanna make option edit product when user login.
Here is the link when user click edit on the web.
<a href="edit.php?id=<?php $id=$data['gameId'];echo $id;?>" ><input type="button" value="Edit"/></a>
Then in edit page, there's submit button, that I fill with some validation in doUpdate.php I tried to get id from URL in doUpdate.php to make an update in database (Id is the primary key, I use my sql as database) so I use this code
$id= $_GET['id'];
The problem is, why $id couldn't take the id value from the url? How should I fixed that? (It is not hosting yet, I use localhost cause I'm still learning)
If I'm understanding you correctly the problem isn't getting the id in edit.php, but getting the id in doUpdate.php. Presumably your edit page is POSTing date to the doUpdate page, in which case you need to do two things.
firstly inside your form on the edit page you'll need to add a hidden element inside you formcontaining the id you want to pass to doUpdate.php
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>
and then in doUpdate.php fetch it from the $_POST global
$id = $_POST['id'];
You have to either edit the form action in your edit.php like <form name="edit" action="doUpdate.php?id=<?= $_GET['id'] ?> or you just create an input field, within the form, which has the value from your GET Parameter.
.
The code inside your <a> tag is incorrect. Also, you can't have a <button> inside an <a>. Try this:
Edit
It is possible for PHP to automatically set variables in this way using a feature called register_globals, but using this feature can open up security holes in your application which is why it is currently deprecated, and will eventually be removed from PHP.
The PHP manual has a detailed explanation of the feature and why it should not be used: http://php.net/manual/en/security.globals.php

html form search query string to be displayed on another page - PHP to do the task

Ok this has had me banging my head for a while, I have posted on other forums but the info is sketchy at best, I hope you guys can help.
Scenerio:
I have a html landing page with a search field. here is the form:
<form method="get" action="URL TO EXTERNAL SEARCH" target=content>
<fieldset>
<input type="text" class="text" name="keywords" value="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"maxlength="30" />
<input type="image" src="images/topnav/btn_search.png" class="button" />
</fieldset>
</form>
So it obviously takes the queried string and off it goes to an EXTERNAL domain which processors the string and returns the result as an HTML page, which in turn displays it in an Iframe named "content" within the same page.
<iframe src="DEFAULT CONTENT" name="content" width="803" height="1200" align="left">
Works a treat, however
PROBLEM:
I have other pages that DO NOT contain the Iframe but contain the search field.
WHAT I NEED TO ACHIEVE:
I need to enter the search string in a html page that DOES NOT contain an Iframe, and display in an Iframe on another html page.
I thought of using javascript/ jquery but due to cross site security issues it doesnt work hence PHP.
I know it can be done but lack the know how.
Any help suggestions would be greatly appreciated.
Thankyou
************UPDATE********************
Cheers for the suggestions so far but I have a few queries:
Thanks for the answer but I have a couple of questions so excuse my ignorance.
"landingpage.html" contains a search box but no Iframe to display the search result. "displaypage.html" contains a search box and an Iframe to display search results.
"displaypage.html" works fine; it sends the query string to the EXTERNAL server which returns a HTML page that I send to Iframe in "displaypage.html".
"landingpage.html" needs to direct the search query to "mysearchhandler.php" which in turn processors the query and returns the resulting html page to the Iframe located in "displaypage.html"
This is what I require to happen, I must have the search option on all my pages without an Iframe and be able to direct them to "displaypage.html"
My problem is lack of PHP understanding, so I am very grateful for your help guys but If possible could you place the code in context to relevant pages, it is easier for me to understand where the code goes and what it is doing.
Iam getting the stage where I am considering paying for the code, but I would like to
Anybody ?
What you could do is have a utility page that handles the search via PHP and returns the expected page.
<form method="get" action="mysearchhandler.php" target=content>
<input type="hidden" name="currentPage" value="thispage.php" />
And in mysearchhandler.php you could user http_get http://www.php.net/manual/en/function.http-get.php to pull down the information from the external search page. Use it with http_parse_message http://www.php.net/manual/en/function.http-parse-message.php
Use the hidden field to tell you which page to return, and update the page to look for the search data.
A simple mysearchhandler.php:
$search = clean($_POST['keywords']);
$currentPage = clean($_POST['currenPage']);
$url = <external search url with get parameters>
$searchResults = http_parse_message(http_get($url))->body;
//Need to validate $currentPage to prevent faked user input
include($currentPage); //Depending on your file system you may need to adjust this
Then in your page you could add:
if(exists($searchResults))
{
<Code to display the results>
}

Categories