Rayeefied Posted December 14, 2009 Share Posted December 14, 2009 Hi! Is there a way to replace a string on a click of a text link? This is what I've come up with. It doesn't work at all ^^;; <head> <?php $string = Red; function changeBlue() { $string=blue; } function changeGreen() { $string=green; } ?> </head> <body> <p>The colour is <?php echo $string ?></p> <br /> <a onclick='changeBlue()'>Blue</a> <a onclick='changeGreen()'>Green</a> </body> I'm starting to learn PHP by myself, and I was hoping someone could help me wrap my head around this problem. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/ Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 PHP runs on the server while onClick events happen on the client and usually execute javascript (which executes client side). To do what you want you'll need to make a round trip back to the server or use Ajax (not a php question). Roundtrip example. <head> <?php if (isset($_GET['color'])) { $string = $_GET['color']; } else { $string = 'Red'; } ?> </head> <body> <p>The colour is <?php echo $string ?></p> <br /> <a href='?color=Blue'>Blue</a> <a href='?color=Green'>Green</a> </body> Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977030 Share on other sites More sharing options...
Rayeefied Posted December 15, 2009 Author Share Posted December 15, 2009 Thank you thorpe. It works! I went on a bit further, and I'm not sure if I should be posting this on a new topic or something, so please tell me to if I should. At the moment, I have a conflict in code. What I want to do is have a page that loads up a flash file, and update a DIV with new text content, all without reloading the page. To change the Flash, what I have done is use your PHP code to replace somethingOrRather in the bits of code that have src="somethingOrRather.swf" And to change the DIV content, I'm using a .innerHTML javascript function. Both ways work fine stand alone, but when I place the two together, it seems javscript executes for a few seconds, and then the php. So it seems like the page 'reloads'? You can see by the example http://ray.eltania.net/TEST/Replace_TEST_07.php Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977578 Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 Because php executes on the server and javascript on the client you need to make a request from javascript to the server (php). This can either be done with a simple page request (and a refresh) or with Ajax (requests are made in the background). Google for an ajax tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977587 Share on other sites More sharing options...
Rayeefied Posted December 15, 2009 Author Share Posted December 15, 2009 Yes Boss! (Seems like the more I want to get into the php, I'm pulled into AJAX instead. lol.) Is there a way to suppress a javascript so it executes last? Sorry, seems like a javascript question here ^^;; Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977605 Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 Is there a way to suppress a javascript so it executes last? I'm not sure exactly what you mean, but php executes first, generating your page, the page is then sent to the client and any javascript it contains is executed. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977609 Share on other sites More sharing options...
Rayeefied Posted December 15, 2009 Author Share Posted December 15, 2009 Based on http://ray.eltania.net/TEST/Replace_TEST_07.php, there's orange box with TEST TEST TEST........... that's a div on the inital page. You can see how when we click on the hyperlinks like test01 and test02, the orange box changes into a grey tall thin box that has "alpha" or "beta" written in it. On my other test, it should be working like this: http://ray.eltania.net/TEST/Replace_TEST_04.php (click on change alpha, change beta, etc etc..) So is it possible that the javascript is executed first, and then the page is overwritten by the php code? Sorry if my question was a bit confusing before. Would like to know why this happens and how it can be fixed/bypassed. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977622 Share on other sites More sharing options...
trq Posted December 15, 2009 Share Posted December 15, 2009 The second example does not use any ajax at all so really, I'm not sure what you are talking about but it has nothing to do with php. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977634 Share on other sites More sharing options...
Rayeefied Posted December 15, 2009 Author Share Posted December 15, 2009 Yep. You're right. The second example doesn't use AJAX or PHP. It's just a simple html & javascript page. It's saved as a PHP page because I doing some experimentation before. But hey.. don't worry about the question. It's getting a bit off topic now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-977669 Share on other sites More sharing options...
Rayeefied Posted December 16, 2009 Author Share Posted December 16, 2009 Hi Thorpe. Just a curious question. The round trip example you helped me with, does that technically "refresh the whole page"? Or does it only refresh just the elements with the <?php echo $string ?>, and nothing else? Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978216 Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 The entire page. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978218 Share on other sites More sharing options...
Rayeefied Posted December 16, 2009 Author Share Posted December 16, 2009 D'OH!!!!!! >___< okay okay. Back to square one. haha...... so anything that doesn't require a refresh has to be done client side, like JS and AJAX? Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978251 Share on other sites More sharing options...
oni-kun Posted December 16, 2009 Share Posted December 16, 2009 D'OH!!!!!! >___< okay okay. Back to square one. haha...... so anything that doesn't require a refresh has to be done client side, like JS and AJAX? Indeed it does. Serverside basically parses it , and serves it to you. Meaning you cannot view it until it is served. AJAX shouldn't be too hard for a simple project that you're using, you can write the backend in PHP and use AJAX to call php. For example AJAX could call 'getpage.php?id=2' and return all the variables you need etc. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978253 Share on other sites More sharing options...
Rayeefied Posted December 16, 2009 Author Share Posted December 16, 2009 Ah... sounds complicated, but I'll try give it a go. Heading over to the AJAX side of the forum now. Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978283 Share on other sites More sharing options...
nafetski Posted December 16, 2009 Share Posted December 16, 2009 Yep - what you're looking to do is Javascript A great place to start (and makes the transition into client side programming a little less painful) is by using a javascript framework. Google "jQuery tutorials" and start from there - should get you up and running in no time. If you have a specific example you need a hand with, I'll be more than happy to help! Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978300 Share on other sites More sharing options...
Rayeefied Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks nafetski! That would be most appreciated Quote Link to comment https://forums.phpfreaks.com/topic/185095-replacing-a-string-on-click-of-a-button/#findComment-978322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.