Passing PHP variables onclick - php

Say I have two files, fileone.php and filetwo.php.
fileone.php generates a list of links, items from a mySQL database, every one of these items have an unique integer ID (1,2,3,4...).
filetwo.php contains a PHP function that uses this ID to create a page displaying the item you clicked in fileone.php. Depending on what item you click in fileone.php you will see a different image, text and more in filetwo.php.
My problem:
How do I at all send the ID from fileone.php to filetwo.php? Do I use forms with the $_GET variable? This is purely a hypothetical situation and I have no code to show, I just need input on how to approach this.

Yes, you could just pass the ID via $_GET
fileone.php...
1
2
3
filetwo.php
if(isset($_GET['id']))
{
//do somethign with $_GET['id']
}
You should look into PDO or mysqli to receive and sanitize the input. do NOT use mysql_ functions as they are deprecated.

Yes, you should use $_GET variable.
Your HTML code in fileone.php will look something like this:
Item 1
Item 2
Item 3
And in filetwo.php you get itemId value with $_GET['itemId'] and output image, text, etc.

Yes, depdending on the sensitivity of the information and wether you care if users may alter the address to see a different id or not, you would use $_GET, otherwise $_POST, and if you are dealing with very sensitive stuff like user states or a shopping cart, then use $_SESSION.
So, for $_GET you would have a link to filetwo.php?id=3
Over on filetwo.php you would connect to the database based on $_GET['id'] and develop your view like that.

Related

PHP - How to use information appended to a URL

Suppose I have a website
www.game.com
Now on this website there is a list of players and when I click on a specific player it takes me to:
www.game.com/stats.php?id=1
Say I go back and click a different player and it takes me to
www.game.com/stats.php?id=2
I need a way for parse_url to extract the values '1' and '2' uniquely when those players are clicked.
If i type out parse_url('http://www.game.com/stats.php?id=2')
This will not work. I need 1 statement that works dynamically to extract that id number and store it in a variable on the stats.php page whenever a players name is clicked.
When a new stats page is loaded the variable is just updated with the new ID and information from my database can be pulled by it.
Thoughts?
These pieces of information appended to the URL are called GET parameters. In PHP, you can access them using the $_GET array:
$id = $_GET['id'] // -> 1 or 2
or you can use $_REQUEST, which includes GET and POST parameters and cookies.
Both of these arrays are superglobals, so you can use them in functions without declaring them as global.
Use PHP GET:
$id = $_GET['id'];
http://www.php.net/manual/en/reserved.variables.get.php

Pagination products with filter parameters

I haven't been working with PHP for some time and now I have to solve a problem about statement of products with pagination and filtration (by price, by the newest ones etc).
This is my url example: web.com/category-name - when I want to paginate products, I use for this purpose the $_GET variable, like: web.com/category-name?p=2.
Now to this basic pagination I would need to add parameters for filtration.
My first idea would be to send form as post and the sent data for sorting save into the SESSIONS and always check, if the parameters for filtration are in SESSION and if they are, then I will filter products.
But I am not really sure that this solution is clean... I don't want to use GET for filter parameters, because there is a lot of possible parameters...
Can I ask you, how would you solve this situation?
I prefer to use POST and save parameters information inside hidden inputs, the problem with SESSION is that if an user closes the browser/tab and goes back it will be where it was instead of the beginning.
Also, if you use the same params in another module (like page=6) of the website it could show another results at page 6 instead of the first one.

Is there anyway to carry a value in php forward to a second page?

I have created a php site, and previously it was listing only products with defined values. I have now changed it to include an array of products for example all products WHERE id = "spotlights"
and this works great so it means I can add new products just to the database, but I still have to add the second page manually. e.g going from the product div on the main page, through to www.example.com/spotlight_1.php
Is there anyway in PHP to carry the data from my index.php e.g. the ID through to the next page? so that I can have a template product.php page, and I can use a database pull to echo the product information required.
So on index.php i click on the product with ID="1" and on the product.php page, it loads the relevant data for product 1.
I can write the php SQL/mySQL calls myself, its just the way to carry accross a value from the previous page which I dont understand
Regards
Henry
p.s.
all the IDs and things are stored in the database already as 1 to 3digit values e.g. 3 or or 93 or 254
Any advice as always is greatly appreciated
Regards
Henry
im not sure if im understading this correctly but you can pass the variable from one page to another using GET variables, or in other words, using the query string in the URL.
So, in index.php you will have links like this:
Product 1
In the second page (product.php) you can get this variable using this code:
$product_id = $_GET['p'];
And then query your database like this:
$query = "SELECT * FROM products WHERE id = '" . $product_id . "'";
$result = mysql_query($query);
Reference: http://www.w3schools.com/php/php_get.asp
Note: Be careful with the way you query your database, the previous code is only a demonstration of how to retrieve the info, but is not a secure solution. I recommend you to check PDO (http://php.net/manual/es/book.pdo.php).
You can use GET if you do not care other users to see your variables, or you can use POST to not let the users what your variables are (useful for password submissions)
One thing to mention is that if you use GET (url?key=value) you need to encode the value using PHP's utf8 enconde function, if you use POST, you don't have to worry about this.
There are a number of ways. You can put the data into session variables, you can POST the data to the 2nd page or pass the data via GET as URL parameters, or you can even save the data in a browser cookie.
Which approach you might use will likely depend on the security requirements for the data and whether you want to be able to access that data strictly via URL (in case of URL parameters).
You can either keep passing the value forward in the request scope until you no longer need it (commonly passed in the url ?var=value), or you can also use sessions if this needs to live longer than the request scope.

Populating PHP page with MySQL info from a variable in the URL

I'm trying to create unique urls for user's of a site I'm designing. Basically I want the url to look like this:
http://www.example.com/page.php?user_id=3
Or whatever user_id they are assigned. Basically I want user to be able to go to that url and it generates the page with the info (first name, last name, etc.) of the user from the row in the MySQL table with that user id number. I know that creating the URL would be done by $_GET but this is sort of backwards from that. I want them to be able to go to the URL and the pages gets the variables from the URL.
Sorry if this is basic, I'm having trouble searching for an answer because it always gives me the answer on how to submit a form TO the URL not FROM the URL. Thanks for all the help.
$user_id = $_GET['user_id'];
you could use this to get the variable.And then search the database for the matching id and get get all the details.
Since you want to contain all the details in you form.You could keep adding the other elements like this.
http://www.example.com/page.php?user_id=3&first_name=Max&last_name=Payne
Using $_GET[which element you want the value of]
Accessing the get variables on the page is really simple!.In your case as you want to access the user id,you can get from $_GET['user_id'] and fire the respective query to fetch the user details to populate the page.For more reference you could check here
http://php.net/manual/en/reserved.variables.get.php

Is this way of doing this the 'better way'?

I have a advertisement website with alot of records stored in a mysql db.
On my main page, I have a form where users may specify their search criteria.
Action is set to myPhp.php file.
myPhp.php contains NO HTML, only php code, and it is basically something like this:
1- get values from FORM and build a SQL query
2- query MYSQL db.
3-display all results in a table using
`while($row=mysql_fetch_array($res))`
4- echo the table.
In this table which is created in PHP, I have several links also, and whenever they are clicked, a javascript function on the parent page is called, which sets a hidden input value to something, and then submits the form again with the chosen variable.
Ex: Whenever users want to go to the next page in the search results, they have to click on a 'next' link created in PHP, and then the javascript gets called, which sets a hidden input value to 'next', and then the form is submitted again, and PHP file GETS the variable from the hidden input and detects that its value is set to 'NEXT' and then displays the next results.
Is there really not another way to do all this ? (that is, a better way when it comes to performance and reliability)
Im still learning so I am very thankful for your help!
I will update this Question whenever you need more input.
Thanks
Replace the next javascript call with a link to
http://yourdomain/myPhp.php?page=2
Then check the get parameter in myPhp.php:
if(isset($_GET['page']) AND is_numeric($_GET['page'])){
$limit = (int) $_GET['page'] * MAX_RECORDS . ', ' . MAX_RECORDS;
} else {
$limit = MAX_RECORDS;
}
//...
$query .= 'LIMIT '. $limit;
Of course you have to change it as your constants change and such.
This method called paginating. Take a look at the Zend Framework's Paginator module to the better understanding.
When creating the link you can use a querystring to specify the parameter that you would like to send to the server rather than submitting a form. So if you have a hidden input in a form called next then you can accompish the same thing like this:
Next
On the myPhp.php page you can get the value of next in a similar way as when you send a form.
If these links have at least one same class across board (with a title attribute) you can attach to them to can attach all these links to a single click event and test to see what title is on the element clicked.
With that you can use a switch to trigger the right function to perform the task (I suppose using JQuery/Ajax).
This should work. At least no hidden fields.
Example:
Next
Ad Details

Categories