Current location: Hot Scripts Forums » Programming Languages » PHP » Passing PHP variables through an iframe

Passing PHP variables through an iframe

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Nov 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Passing PHP variables through an iframe

Can you do an IF statement to pass a variable from one page to another page that opens within an iframe?

I am working on a car dealership site that has a "new arrival" picture on it. When the picture is clicked it is supposed to go to inventory.php, which has an iframe containing this:

HTML Code:
<iframe src="inventory/new_inventory.php" width="725" height="840" name="showlistings">
But I need it to open up inventory/new_vehiclelisting.php with the ID of 155 (the id of the car from the MySQL db) ONLY when the new arrival button is clicked.

What i need is something like: IF new arrival picture is clicked then go to inventory.php and display within the iframe new_vehiclelisting.php with id 155 instead of new_inventory.php.

Any ideas?


the site is here: www.auto-quest.com/new/index3.php and the page in question is inventory.php, which has the iframe containing the inventory pulled from the database.

Last edited by Orid; 08-23-08 at 03:58 PM. Reason: link to site
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
PHP relies on an http request to the server to do anything.

What you want to use is Javascript.
You can assign a new address to the iframe with GET or POST values attached to it.
Thus enabling the page that is displayed in the iframe access to the values through $_GET[] or $_POST[] arrays.

When you goto inventory.php you want to send the values you need through the "new arrival" picture link.
(ie: http://inventory.php?id=155 )
Those values can be retrieved by Javascript on the inventory.php page and an ammended address can be assigned to the iframe by Javascript.

The Javascript function can be called if there is a value of 155 in the $_GET["id"] variable..
__________________
Jerry Broughton

Last edited by job0107; 08-23-08 at 08:30 PM.
Reply With Quote
  #3 (permalink)  
Old
Newbie Coder
 
Join Date: Nov 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
How would the javascript syntax for this look?
Reply With Quote
  #4 (permalink)  
Old
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 413
Thanks: 12
Thanked 3 Times in 3 Posts
well i would use something similar, if u make an if on the inventory page that if the get id=0 then just list all but if the id= 155 in this case then show id 155 so something like this

PHP Code:

$invet_id $_GET["id"];
if (
$invent_id == ""){
echo 
" YOUR LIST ";
exit;
} else {
echo
" Unique Id ";
}
exit; 
that should do it

let me know
mike

Last edited by UnrealEd; 08-25-08 at 03:29 AM. Reason: please use the [php] wrapper when posting php code
Reply With Quote
  #5 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Ok,
Your going to have something like this for your "new arrival" picture link:
HTML Code:
<a href="http://inventory.php?id=155"><img src="new_arrival.jpg"></a>
And then on the inventory.php page you have your iframe.
So on inventory.php you put in the Javascript code to change the page the iframe displays.
Something like this:

inventory.php (sample)
PHP Code:

<html>
<head>
<script>
function change_page(v)
{
 document.getElementById("showlistings").src = "inventory/new_vehiclelisting.php?id="+v;
 }
</script>
</head>
<body>
<iframe id="showlistings" src="inventory/new_inventory.php" width="725" height="840" name="showlistings"></iframe>
<?php
$the_id 
= !empty($_GET["id"]) ? $_GET["id"] : "";
if(
$the_id == 155)
{
 echo 
"<script>change_page(155);</script>";
 }
?>
</body>
</html>
The only thing with this code is that you must fetch the id value from $_GET["id"]
when you get to the new_vehiclelisting.php page.

So if your using like $_POST["id"] then you can load that variable like this:
PHP Code:

$_POST["id"] = $_GET["id"
This of course only applies to the new_vehiclelisting.php page.

Understand?





Or, for your "new arrival" picture link, you could have something like this:
HTML Code:
<a href="http://inventory.php?id=155&new=true"><img src="new_arrival.jpg"></a>
This would allow you to put any value you want for the id.

Then you would use this code for the inventory.php page:
PHP Code:

<html>
<head>
<script>
function change_page(v)
{
 document.getElementById("showlistings").src = "inventory/new_vehiclelisting.php?id="+v;
 }
</script>
</head>
<body>
<iframe id="showlistings" src="inventory/new_inventory.php" width="725" height="840" name="showlistings"></iframe>
<?php
$the_id 
= !empty($_GET["id"]) ? $_GET["id"] : "";
if(!empty(
$_GET["new"]))
{
 echo 
"<script>change_page(".$the_id.");</script>";
 }
?>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 08-25-08 at 11:09 PM.
Reply With Quote
  #6 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
You can simplify the previous code a little like this:
HTML Code:
<a href="http://inventory.php?id=155&new=true"><img src="new_arrival.jpg"></a>
PHP Code:

<html>
<head>
<script>
function change_page(v)
{
 document.getElementById("showlistings").src = "inventory/new_vehiclelisting.php?id="+v;
 }
</script>
</head>
<body>
<iframe id="showlistings" src="inventory/new_inventory.php" width="725" height="840" name="showlistings"></iframe>
<?php
if(!empty($_GET["new"]))
{
 echo 
"<script>change_page(".$_GET["id"].");</script>";
 }
?>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Or , you could simply do it like this:

HTML Code:
<a href="http://inventory.php?id=155"><img src="new_arrival.jpg"></a>
PHP Code:

<html>
<head>
</head>
<body>
<?php
if(empty($_GET["id"])) {
?>
<iframe src="inventory/new_inventory.php" width="725" height="840" name="showlistings"></iframe>
<?php
}
else {
if(
$_GET["id"] == 155){
?>
<iframe src="inventory/new_vehiclelisting.php?id=155" width="725" height="840" name="showlistings"></iframe>
<?php
 
}
}
?>
</body>
</html>
Or like smithygotlost said:
PHP Code:

<html>
<head>
</head>
<body>
<?php
if(empty($_GET["id"]))
{
?>
<iframe src="inventory/new_inventory.php" width="725" height="840" name="showlistings"></iframe>
<?php
 
}
else {
echo 
'<iframe src="inventory/new_vehiclelisting.php?id='.$_GET["id"].'" width="725" height="840" name="showlistings"></iframe>';
}
?>
</body>
</html>
This should give you plenty of options.

If you are concerned about people having their Javascript turned off then you can use one of the PHP only solutions.
But I wouldn't be too concerned because most people have their Javascript enabled.

We are in the era of Web 2.0, where millions of people use GOOGLE for their preferred search engine.
And GOOGLE relies on Javascript to function.

Not to mention the growing popularity of AJAX, which is a part of Javascript.
__________________
Jerry Broughton

Last edited by job0107; 08-26-08 at 12:11 AM.
Reply With Quote
  #8 (permalink)  
Old
Newbie Coder
 
Join Date: Nov 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Wow, thank you guys so much for this! It works great! I ended up using the if empty GET id method, and it's exactly what I needed.
Reply With Quote
  #9 (permalink)  
Old
New Member
 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
to job0107 : thank you for your sharing.. i'm newbie on this forum. i would like to learn the web programming language more.. keep share.. my problem now finished because this article... thank you..

to Orid : thank you for your case, guys.. the case is same with my problem on my project.. keep share..
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing variables from form to iframe corman420 JavaScript 1 07-21-08 09:13 AM
PHP variables to microsoft word Deansatch PHP 7 12-17-07 02:21 PM
php benchmark: variables with casts vs variables without casts markg85 PHP 3 09-04-07 02:19 PM
Pulling Variables from php deeaamon JavaScript 1 12-29-05 12:29 AM
js function / border & iframe cityonfire JavaScript 1 07-24-05 08:02 PM


All times are GMT -5. The time now is 06:37 AM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.