Exchange variables between two pages without changing the page - php

I am aware that this kind of question has already been asked before.but my case is i want to send a value to another php page, and get the value from it without changing the page. is that possible ? and if it is. can i have an example ?.

You can use AJAX as Jeff said.
Here's an example:
PHP - AJAX and PHP

You can use session in this case
in which one page will send the data to another page

Related

PHP : How to redirect to the same page while retaining data from another page

I think the question is a bit confusing but technically I am passing a value from one page :
Update
As you can see when I click the link "Update" I will be redirect to the next page (Admin-updateGamesFunctions.php) while passing the value gameID. I'm receiving the value by using :
if(isset($_GET['gameID']))
{
$updateGame = $_GET['gameID'];
}
Now everything about the page works perfectly. However, when I'm done submitting the form I try to redirect back to the same page while retaining the gameID I got from previous page.
This here is my current code for that :
header(" location : Admin-updateGamesFunctions.php?gameID=".$updateGame);
However when it redirects all I got is :
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
P/S : Not sure if this is needed but I'm avoiding including the previous page due to reasons.
Edit :
Doing what #Felix Mellitzer said does help in removing the error. (Need to remove the header first)
Anyway the reason I'm trying to retain the gameID is because I'm using echo at the page that shows all the attributes of the object (games) based on its gameID. So I was hoping that after I done submitting the form (which updates the attributes of the game) it will also update the data echoed on the page. However the data echoed on the page is not updated.
Edit 2 : I already found out how to update the echoed data. Just use
header("Refresh:0");
Thanks everyone!
When you submit the form, the $_GET['gameID'] is lost. Change your form URL to include the gameID.
Example:
<form action="/action_page.php?gameID=<%= $_GET['gameID'] %>">
...
However as #Ajeenckya already said. You should use something like the session. By using sessions, you don't have to pass the parameters all the time.
Try using PHP sessions $_SESSION for managing data across pages in PHP.
https://www.php.net/manual/en/reserved.variables.session.php

Getting a variable passed from one page to another using PHP

I'm hoping someone can help me. I am trying to just pass a variable from one page to another in my jQuery mobile website, which you can see here: http://www.muskermcintyre.co.uk/tablet.
The usual way I would do this is the following:
To have a link on the list view page which looks something like this:
View details
Then I would need to use $_GET['profileID'] on my full details page, to get the correct property from the database, obviously just getting that profileID which has been passed from the link on the previous page. It seems this is not possible in jQuery mobile.
Please can anyone suggest the best way to do this?
Thanks!
You can use the same method for your JavaScript as in PHP, the only difference is that you don't have the query string variables as an array named $_GET, you have to parse them yourself. See this answer:
How can I get query string values in JavaScript?
PHP is a server side scripting language.
Once your page is loaded (i.e HTML is generated). You will have to use either JavaScript or JQuery.Or you will have to post back to server to render new HTML.

Using jquery to set php variable value?

I currently have 2 pages:
Follow.php:a PHP page which is all setup with twitters api and will follow any user on twitter with the specific parameter you put in.
One index.php page, which has a list of links of people you can follow.
What I am looking to do is have it so when a user clicks on one of the people to follow on index.php, the variable in follow.php (which is used as a parameter and is the username of twitter account that is going to be followed) gets set to the name attribute of the link selected.
Now I am not the greatest developer, so I am not sure of the best way of doing this. There is probably a better way of doing it that what I am thinking of. I was thinking of using jquery to say something like:
$('a').click(function(){
<?php $group=?>$(this).attr('name'); //trying to set a php variable to name attr
})
This was going to be in its own new file,
then I was going to include this script into the follow.php script and use the variable. This is not working, unsurprisingly. I hope I explained what I was trying to do well.
So I have two questions.
Number 1:what is the best way of achieving what I am trying to do? can i do this all in php?
Number 2: is it possible to do what I was trying to do with jquery and setting the php var equal to the name attr?
PHP runs on the server. JavaScript (jQuery) runs on the browser. PHP can be used to write JS, but JS can't affect PHP (except via AJAX or similar).
What you need to do is do a POST on the Follow.php page and accept the username value. This is a great resource for that: http://www.html.net/tutorials/php/lesson10.php
You can simply set the link href for each tag on the index page to go got Follow.php?user=twitterHandle (where twitterHandle is the twitter username of a person). This way, you dont need to use any javascript at all.

Pagination together with a html form

On my website I use a pagination (similar to that one on the bottom of this page) for MySQL output. For the change of the current page I use GET method (variable page) and it works well.
However, on my page I have also a form, using method POST, which acts as a filter for the MySQL output. This rises a problem because, when I change the form settings an submit them (POST), the page in the address line (GET) remains the same. This is problem in some cases when the filtered output has less pages than that one currently set.
Is it somehow possible to set the page variable to 0 always when the form is submitted?
Particularly, I did it using $_SERVER['REQUEST_METHOD'] == 'POST'. However, this changes just the variable in the code. Not at the address line.
On the other hand I want to keep the POST variable when I change the page of the output.
Thanks in advance.
There is a logical collision in your setup:
Unlike GET method, POST method doesn't keep variables in the address bar. But for some reason you are using POST method.
So, the solution is quite simple - use GET method for the filtering.
To create pagination links use http_build_query() out of $_GET array
In fact it's better to send filter criteria in the address (GET method). It will solve your problem with pagination and you (and your users) will have a direct link to search results.
In your PHP code, you want to redirect to the URL with the page GET parameter removed if you are SUBMITTING (by pressing the "Filter" button, or whatever it's called). So it will start at page 0.
You would have to rewrite the URL yourself (or using a plugin).
Typo corrected, I meant page 0, not 1 :P

POST variables disappearing when a link is clicked

I just setup some pagination for a search, and the search uses POST variables to define what to search for etc. In my URL I can set the pagination offset like this search/OFFSET, and my links in the pagination link there correctly. However, when I click a link all POST variables vanish even if I explicitly set them so I can use them in the next script. I'm using codeigniter and I have GET turned off and really don't want to store these 5-6 values in a session since then it will get all clumsy.
Does clicking a link fully reload the page and delete POST variables?
Thanks
Yes, clicking a link creates a GET request so wouldn't keep any of the POST data. Although it's technically possible to do so with javascript, that's a bad idea.
This is an entirely appropriate use of GET, please read this fuller explanation.
Yes, clicking a link removes all the POST variables.
Do you have access to change your php page that receives the request? You might want to adjust your variables there to accept either GET or POST:
$defaultvalue='';//change this to '' or NULL or whatever you want
$searchQuery = (isset($_POST['s']) ? $_POST['s'] : (isset($_GET['s'])?$_GET['s']: $defaultvalue));
Then your php page will be better equipped to handle either GET or POST
POST data will only be present during the original request (i.e. it does not persist between requests). If you want data to persist, use sessions. However, it is common practice to use GET for search queries and pages.
You could use an incredibly ugly workaround and set a form full of hidden fields to submit when you click a link. I really wouldn't recommended it though.
You should be able to create a form that submits some set of post variables to the action parameter with the get variables. So the form should submit a post request to http://www.somedomain.com/FormSubmit.php?pag=1&sort=asc This would submit the post values of that form along with the get values of the string. If you can change your link to a form button, you should be good to go.

Categories