Jump to content

Replacing a string on click of a button


Rayeefied

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.