How to put php GET params in a HTML file? - php

I have a PHP file which returns me a JSON array based on the GET parameters set. I don't want to reload the page so I made a simple function using jQuery + AJAX to show the result in a div tag.
But there is a problem, because of using AJAX doesn't change the URL and the user won't be able to share the link.
The HTML is called 'calculate.html' and the GET URL would be something like this:
myweb.com/calculate.php?sex&female&age=23&brand=pull%26bear&msg=hello%20stackoverflow
I'd like to know what is the best practice to modify the URL of the HTML when the AJAX call returns the result. And when someone open this URL the HTML reads it and then executes the call with these parameters.
I've modify the .htaccess and this is the url of the html:
myweb.com/calculate/
And with the parameters:
myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow
Any tip, advice or help would be appreciated, and if you need more information, let me knoe and I'll edit the post.

You can use a # tag in your url
eg http://yoursite.com?param1=23#tempcode
So you can call the ajax code when the url has the #tag tempcode.
In this way you can share your new url

On complete of your AJAX request, you can redirect the page url using
window.location.replace("myweb.com/calculate/female/23/pull%26bear/hello%20stackoverflow");

Related

How can I redirect to a PHP page with an <a> tag? Also how can I pass an ID number through the URL with it?

For my PHP project I am using a post method for the form and I want to be able to redirect to a PHP page with an <a> tag.
This is the code that I want to use to redirect, but I don't know how to make it work with the <a> tag.
$query_string = ['item' => $_SESSION['items']];
$url_query = http_build_query($query_string);
header("location:detailPage.php?$url_query");
Also, How can I pass an ID number through the URL from the current page to a different PHP page?
You can use html tag with embedded php code
TEXT
in detailPage.php you will call it like
if(isset($_GET['id'])//getting id from url using $_GET request
{
$id = $_GET['id'];//storing in php variable
}
I hope this will help you. If there is anything wrong kindly let me know

How to make a window pop up from a link using javascript

I am making a bookmarklet for my url shortener. What it does is it shortens the current url of a page when you click on the bookmarklet, so you don't have to go to a site to shorten a url, but what I need is for the bookmarklet to return a pop-up window with the shortened url listed below.
What I've done so far is this:
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u;window.open(s,'shortened','location=no,width=400,height=300');
If you have bookmarked this, you will notice when you click on it, it does shorten the url of the current page, but it also shows the url shortener webpage, and what I only need is plain text and a link to copy it.
Please help!
Edit: Sorry I'm that bad at php, but how can I make a parameter "responseType"? Thanks
What is shown in the popup is what is returned by http://n1x.co.uk/create.php. You will have to modify the php to return only the shortened url in plain text. If the php is being used by the shortener site also, what you can do is pass a parameter to create.php that tells it whether to return the whole page like it does now or to return only the plain text url. For example a parameter called 'responseType'. You can modify the create.php to say that when responseType = 'plainurl', return simply the shortened url and not the whole page.
javascript:u=encodeURIComponent(location.href);s='http://n1x.co.uk/create.php?url='+u + '&responseType=plainurl';window.open(s,'shortened','location=no,width=400,height=300');

Http post/get dynamic action url

Hello is it possible if someone could tell me the easiest way to create a dynamic action url on a html form.
We have 2 text box's and we would like to submit the information to a url like the following http://example.com/box1/box2/ but using the values contained within the forms text box's.
I guess this would need to be done via java script or something but i have no idea as to how to go about it.
Any help on this would be appreciated
thanks
It's very simple. All you need to do is to get the values from your script and then use header redirect. e.g.
if(isset($_POST['SUBMIT'] {
$box1 = $_POST['box1'];
$box2 = $_POST['box2'];
header("Location: http://example.com/$box1/$box2/");
}
or you can use PHP curl library if you do not want to do a redirect
if using javascript, guess u could do something like this, php or server RewriteRule (.htaccess) might be what u r looking for.
<script>
//replace the textbox1 and 2 with the id of your text boxes in your form
var box1=document.getElementById('textbox1').value;
var box2=document.getElementById('textbox2').value;
window.location ="http://example.com/"+box1+"/"+box2+"/";
</script>
Might want to put the script into a function and call it with an 'onclick=' event or if u want to call the javascript before the form submits. try using 'onsubmit=' with return=false; at the end of the function.
hope it helps

Getting source code from page AFTER jQuery function in PHP

I'm trying to get the source code from this page, to get the code of the timetables, and then parse it.
But when I fetch it with file_get_contents, the div I'm looking for is empty. I searched in the code, and it looks like it's filled with jQuery when to body is loaded.
So my question is : how can I get the source code of the page after the jQuery is executed ?
Thanks.
Since jQuery is javascript, php's file_get_contents will not help because it does not execute javascript.
Either find out which ajax data they load and use them directly, or use a browser ;)
the information you want comes from this page http://www.cinesion.ch/cinesion/timetable.php through an AJAX request so file_get_contents will never get it unless you do the call directly to this page...
try this one
http://www.cinesion.ch/cinesion/timetable.php?date=%&movie=%&city=%&_JEXEC=1

Variable from JavaScript to PHP

Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

Categories