Jump to content

Triggering PHP Variable from JavaScript Variable?


paulmo

Recommended Posts

Are you using the session? The idea was, on each page, first check if the value is set in the session. If it's set in the session, then use that value, you don't need to do anything else if it's set in the session. If it's not set in the session, then get the value (however you want to get it), and put it in the session. If they change the value from a dropdown, put that in the session too. The point is to use the session so you don't need to keep trying to get the value on every page, it's going to be saved.

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply
That is EXACTLY right. What is the misunderstanding? Yes! and as full name "Maine" etc. which is what I need, and that's how it's searched against column in db table. Through trial and error, I have isolated the problem to this section of code:
if(isset($_GET['statelink'])){   //this is working 	$state = $_GET['statelink'];   //working, triggers query, etc. }else{  $state = $_POST['$geoplugin->regionName'];  /*or $_GET, not working, OR, it's just not "triggering" the query the way 'statelink' is triggering the query, because the latter comes from a form? I've also tried it without $, and ['" .$geoplugin->regionName . "'] etc...*/};

And to clarify, this DOES work:

echo $geoplugin->regionName;

And I am getting no errors.

you're missing the point of the way the conditional is supposed to work. You don't need to be checking for $_POST in the else part. That part is only for determining the state based on geo_locate. Just follow my example...(again :))
if(isset($_GET['statelink')){  //the form must have been submitted, use submitted value  $state = $_GET['statelink'];}else{  //form not submitted, use geo locate instead  $state =$geoplugin->regionName;};//now use $state to execute your query and populate the page like you would normally

If the form has been submitted then isset($_GET['statelink']) will return true, and thus $state will be set to that value. If the form hasn't been submitted, like on page load, then isset($_GET['statelink']) would return false, and thus you would want geo_locate to get the state. Now that you have a state, do the query and all the data substition, (outside!) of the if/else statement.

Link to comment
Share on other sites

or you can use $_SESSION in a way the JSG is suggesting as well, but I think you should learn/finish it this way (for now) as we've been working towards that direction. Adapting it the way JSG proposes would only take a couple of lines, however.

Link to comment
Share on other sites

Scientist, I'd say I love you but that might sound weird. So big thanks. Do you guys "own" this site? How to you get compensated for your instruction here? I was confused in your isset script before $state = geo_locate_return value(); because I got a function error, so now I see I was overthinking it. JSG, I'm going to read about SESSION and how that would work here as well as following a previous link you posted. Should I still do sesssion now that it's working?

Link to comment
Share on other sites

yes, basically, read up about how $_SESSION works. It is is similar to $_GET/$_POST in that it is a global array, that can have data saved in it, but unlike $_GET/$_POST it can persist throughout a users time on your site. However, you must call session_start() on each page which will need access to this $_SESSION array. However, since you are keeping all of this on one page, tracking $_SESSION shouldn't be a big deal. Although it appears that you want people to be able to change states over the course of their time on the page(s), I think what we discussed would be sufficient, however, this is what I think JSG is thinking...

if(isset($_GET['statelink')){  //the form must have been submitted, use submitted value (user is changing the state)  $state = $_GET['statelink'];}else{  //form not submitted  //use geo locate or session instead (could be first time page visit)  if(!isset($_SESSION['statelink']){	//must be a first time page load (nothing saved in $_SESSION), we need to use geo locate to get the state	$state = $geoplugin->regionName;  }else{	//$state has already been saved in the $_SESSION, use it's value	$state = $_SESSION['statelink'];  };};//makes sure the latest value of $state gets saved to the session array$_SESSION['statelink'] = $state;//now, perform query, etc

The benefit of this approach is that if you have a site full of pages that rely on the users state to display any sort of information, having the state stored in $_SESSION mean's that if they go to another page, you can use the value saved in $_SESSION instead of needing to geo_locate (although I'm not quite sure what the 'fuller' benefit is) but I'm sure JSG will elaborate.Also, without the comments, that above code example can be quite condensed

if(isset($_GET['statelink')){  $state = $_GET['statelink'];}else{  $state = ((!isset($_SESSION['statelink']) ? $geoplugin->regionName :  $_SESSION['statelink']);};$_SESSION['statelink'] = $state;//perform query, etc

just an example, so I may be missing JSG's points considerably, haha. No one that I know of that posts here (since my join date) is directly affiliated with W3schools. JSG is a moderator, but I have no idea regarding his contact with the forum admin(s) or the owners of the W3Schools site. No compensation is requested/given for our time. We're just here to learn and help, like everyone else. We all have day jobs that involve some sort of development work, some just have higher skill sets than others.

Link to comment
Share on other sites

I'm getting great results here with the state page coming up (Maine for me), but results elsewhere seem to be a problem :). Any idea how to troubleshoot this?
you'll need to be more specific. On other pages in your site? Different users in different places viewing your site? What is the nature of the problem?
Link to comment
Share on other sites

The nature of the problem is that while I get my state here at home, even after deleting cache, etc., NO ONE (not even neighbors, turns out) is getting the geolocate action, which triggers page content, etc. So they're just getting HTML with gaping holes where content needs to be. The geolocate script I use uses cURL; I'm wondering if somewhere in the line of JS to PHP things break down, like if folks have JS turned off. But that's probably not it, because everyone I've checked with is your run-of-the-mill user, Windows, IE, email, etc.Oh the dropdown works fine.

Link to comment
Share on other sites

do you have a live link? what about the geolocate documentation? Does that have anything, like an FAQ or developers forum?

Link to comment
Share on other sites

Yes the author was responsive earlier about a question, so I'll try that as well. In the meantime, here is the live link; warning, I'm doing the affiliate thing. There should also be an online brokerage banner in the center of page that not everyone is getting. Thanks in advance.

Link to comment
Share on other sites

it worked for me. I live in Rhode Island and this is what I see at the top of the page

Rhode Islanders know yachting and Narragansett Lager, and they also know a good value. Catering specifically to those earning $40K or less in salary, ReleaseCenter.org tracks up-to-the-minute [04 Nov 2010 14:55 ] values in personal banking, automobile and health insurance, and career education in Rhode Island. If you live outside Rhode Island, select your
and then I choose Arizona and that worked for me.
Link to comment
Share on other sites

Wow at least that's good...thanks. Ideas to troubleshoot this? Would putting $geoplugin->regionName in $_SESSION be more consistent? Just deducing from feedback, though, this seems to be a local browser issue.BTW JSG just got around to reading your Tips and Tutorials on SESSION and putting all the PHP code at the top of the page because that runs before anything else. Great!

Link to comment
Share on other sites

We have guns. Lots and lots of concealed weapons which you don't need a permit to conceal. You can walk down the street with a rocket launcher under your coat and not get arrested. I got pulled over by a cop and he asked me if I had a gun. I said no, and he said "Why not? Take mine!".Our best known beer is probably Four Peaks. I'm not sure what kind of distribution they have, but everyone loves KiltLifter.

Link to comment
Share on other sites

So Rhode Island gets beer and we get scorpions, huh? I see how it is.
We have guns. Lots and lots of concealed weapons which you don't need a permit to conceal. You can walk down the street with a rocket launcher under your coat and not get arrested. I got pulled over by a cop and he asked me if I had a gun. I said no, and he said "Why not? Take mine!".Our best known beer is probably Four Peaks. I'm not sure what kind of distribution they have, but everyone loves KiltLifter.
:)you guys got it made! Although the political scene there scares me.
Link to comment
Share on other sites

Shared this with a friend last night for a laugh:

I got pulled over by a cop and he asked me if I had a gun. I said no, and he said "Why not? Take mine!".
Sounds like I need to edit AZ. I went to UW Madison so I know the culture first-hand. RI not so much, well the politics are notorious, but I left that alone for Narragansett. But are you ALL getting your state page on first visit? Maybe you're all on Linux, Iceweasel, etc? The guy who wrote the geolocation script is in Switz. and he gets Cotes d'Azur on page load, which is pretty cool, except of course the PHP content is only for US states. My friend, on Verizon Wifi, gets New York page, in Maine. :) At least it's a state. The real problem is that some are getting no state info loaded, so "Financial Solutions...in the State of [blank]. Ouch. If they don't get state, then there are also content holes elsewhere.
Link to comment
Share on other sites

I'm running on OSX. I imagine the answer is in the geolocate script itself and how it performs the lookup, and possibly has to do with each users ISP.

Link to comment
Share on other sites

OK, should I put an elseif in here and define $state and $description variables for that condition, to provide default content for those who don't get the geolocate?

if(isset($_GET['statelink'])){	$state = $_GET['statelink'];}else{	$state =$geoplugin->regionName;};

Link to comment
Share on other sites

I'm running WinXP. I got my state right away. I agree with scientist though, the issue is probably in the geolocate script.

OK, should I put an elseif in here and define $state and $description variables for that condition, to provide default content for those who don't get the geolocate?
if(isset($_GET['statelink'])){	$state = $_GET['statelink'];}else{	$state =$geoplugin->regionName;};

That would probably be a good idea. You don't necessarily have to set a default state, but you should have some default generic content so that you don't have gaping holes in the page.
Link to comment
Share on other sites

I'm running WinXP. I got my state right away. I agree with scientist though, the issue is probably in the geolocate script.That would probably be a good idea. You don't necessarily have to set a default state, but you should have some default generic content so that you don't have gaping holes in the page.
I would figure out a way to test the return value of the geolocate script, so that if it returns a valid state, then you should execute the query using that return value, else use a default state as a backup. One way to test the return value is to put all the states in an array, and then compare the return value of the geolocate script to each value and figure out what to do from there. i.e.
$default_state = "Rhode Island";if(isset($_GET['statelink'])){	$state = $_GET['statelink'];}else{	$state = $geoplugin->regionName;	//some sort of function to test $state based on it being the return value of geolocate	$check = validateState($state);  //could return something as simple as bool true or false   	//if validateState() returns true, use $state, else use $default_state	$state = (($check) ? $state : $default_state);};//now you can use $state in your query like you were already doing, and it will always 'validate' to one of the 50 states

Link to comment
Share on other sites

Thanks for getting me started again. Help me understand. You're proposing 2 if/else statements? If so, why not if/elseif/else? Or one of those ternary deals with a switch? Your 1st if/else:

if(isset($_GET['statelink'])){	$state = $_GET['statelink'];}else{	$state = $geoplugin->regionName;

2nd if/else:

//if validateState() returns true, use $state, else use $default_state	$state = (($check) ? $state : $default_state);

about the $check = validate function, is that where I'm putting an array naming the 50 states, then doing an if 0 false then $default_state? I won't have to do anything with the 1 value there though because if the $geoplugin were detected, it would have run earlier, right? I guess I don't see why we can't shorten the whole thing to if get statelink, else if geoplugin, else default. What am I missing? Thanks A LOT!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...