Passing vars from parent to iframe

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
Hello all, I've searched and read extensively on this but havn't found something that I could make work.

I have a php page on which a number of results are displayed from a mysql database query. Some of these results are hyperlinks (the 'name' var). On the same page is an iframe. When the hyperlink 'name' is clicked, I would like the iframe to load with the appropriate fields from my database ('name', 'location', 'address', etc).

I only want to pass one variable from main page to the iframe, and from there i can build the mysql query to return the results i need.

So how do I pass the var 'name', which is a result returned from the mysql query, to the iframe?

I've been playing with passing the var through the iframe url, but can't seem to find a way (with my limited knowledge :)) to pass it, considering that the hyperlink that loads the iframe looks like this:

echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php')\">" . $row['name'] . "</a> </td>";


Thanks for any help, I appreciate it.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I'd need to see the code behind loadintoIframe() in order to see how it's loading the page to be sure of this. However, I believe that just changing iframe2.php to iframe2.php?name=thename would be adequate. Then the script for iframe2.php should have the variable $_GET['name'].
 

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
Thanks for the response, it works as you suggest. When I do the following script:

echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php?name=examplename')\">" . $row['name'] . "</a> </td>";


the iframe will display "examplename" when the hyperlink is clicked. Fantastic!


Now, I wish to take this one step further. Since the hyperlinks that load the iframe are themselves created by a php query of a mysql database, I want to dynamically insert the 'name' var into the hyperlink. However, when I try the following code:

echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php?name=<?php echo . $row['name'] . ?>')\">" . $row['name'] . "</a> </td>";


the page returns a PHP error, which says Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/jamieg/public_html/bar/barsTEST.php on line 127

D
o I have a problem in my syntax again or is this simply not possible?

Thanks AGAIN for any help! I am learning a lot through this.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
You'll get that error if you try to place an associative array variable in a string without enclosing it in curly braces. Also, there's no need for the <?php ?> tags in the string. This should work fine:

echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php?name={$row['name']}')\">" . $row['name'] . "</a> </td>";
 

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
woiwky, you're the man! That works beautifully! I am learning a lot about syntax here...

Can I ask how you pick up so much about the programming language? Do you work in the field, did you study it, or have you just picked it up along the way?

Thanks again for solving another of my big issues. This resolves one of the big hangups I was facing. I'll let you know when this site launches.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Actually I think he's showing off :p
PHP:
echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php?name={$row['name']}')\">" . $row['name'] . "</a> </td>";
is exactly the same as
PHP:
echo "<td> <a href=\"javascript:loadintoIframe('myframe', 'iframe2.php?name=".$row['name']."')\">" . $row['name'] . "</a> </td>";

Tips for learining php?
1: use it a lot
2: look at examples a lot
3: don't forget numbers 1 and 2
4: read more tutorials then just one
5: get a program that's better then notepad...
=> notepad++ is the best to my belief ^^
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Maybe a little bit of showing off and little bit of nothing better to do :p I figure if I know the answer to a question, I should say it. The sooner it's answered, the better. Right? ;-)

But anyway, marshian's right. The best way to learn a language is to use it. I personally never use code made by other people, regardless of what language I'm using. If you make everything yourself from scratch, you learn a lot more a lot faster, and the language stays fresh in your mind.

Not that I look down on people who use third-party code to write their apps. I understand that some people don't have the time or desire to fully learn a language, they just want to utilize it. But for people who want to actually become a developer, I don't recommend it. Most of the time, writing it from scratch means better code efficiency, and much easier modding.

Also, you'll notice that once you learn your first language, learning others becomes a lot easier. Since it's just a matter of associating how you do something in one language to how you do it in another.
 
Top