Page 1 of 1
Isit possible 2 change the value of a variable using events?
Posted: Fri Jul 22, 2011 3:35 pm
by drayarms
Hello all. Well the topic pretty much sums up the problem. I'm pretty familiar with using events to alter the value of an html tag or attribute via the DOM. I was wondering if it is possible to use an event to alter the value of a variable within. Well I wrote the script below to test that and sure enough it didn't work. So if by some chance it is possible to use events to do alter the value of a variable, how does one go about that?
Code: Select all
<?php
$variable = "1";
echo "<script type= 'text/javascript'>
function changeValue() {
$variable = "2";}
</script>
<a href=''> <h3 onclick = changeValue() > Click here to change value to 2 </h3> </a>
";
echo $variable;
?>
Re: Isit possible 2 change the value of a variable using eve
Posted: Fri Jul 22, 2011 8:29 pm
by McInfo
Just as you won't find construction workers or tools scattered around at the grand opening of your new restaurant, you won't find PHP functions or variables in your freshly-generated HTML page.
Don't like the floor plan? Well, you're going to have to ask the construction workers to tear down the restaurant and build you a new one. That's like refreshing the page, clicking a link, or posting a form.
Maybe your only complaint is the color of the paint. You don't have to rebuild the whole restaurant. You can call back the construction workers and have them give you new paint. Likewise, you can use AJAX to request a fragment of HTML, or data formatted in some other way, that can be used to modify the existing page.
Construction workers are busy people. They have other clients, and they can't wait around for you to think of jobs for them. As soon as the job is done, they're gone. If you want them to use the same color next time they paint your walls, you are going to have to remind them or hope they kept a color swatch in their records. Think query strings, cookies, and sessions.
Re: Isit possible 2 change the value of a variable using eve
Posted: Fri Jul 22, 2011 9:30 pm
by califdon
@McInfo: nice analogy!
@drayarms: A PHP variable exists on the server because PHP is a server language. Any event that Javascript could detect would be after the HTML and Javascript have been received by the browser because Javascript is a browser language. All PHP can do is send it to the browser. Using AJAX, which is basically just using a particular method of Javascript, you can go back to the server and get more data, but the real answer to your question is flat No, you can't directly affect a PHP variable by an event that happens AFTER the server has completed its job and the variables of that PHP script no longer exist.
Re: Isit possible 2 change the value of a variable using eve
Posted: Sun Jul 24, 2011 2:10 am
by drayarms
@mcinfo, that was a wonderful analogy, not only made me realize how stupid my question was but also made me chuckle a bit. i guess i'd have to figure out what extra tools i'd ask the construction workers to leave behind before their contract is over. @califdon, thanks too.
Re: Isit possible 2 change the value of a variable using eve
Posted: Sun Jul 24, 2011 12:47 pm
by McInfo
Re: Isit possible 2 change the value of a variable using eve
Posted: Sun Jul 24, 2011 1:27 pm
by califdon
drayarms wrote:@mcinfo, that was a wonderful analogy, not only made me realize how stupid my question was but also made me chuckle a bit. i guess i'd have to figure out what extra tools i'd ask the construction workers to leave behind before their contract is over. @califdon, thanks too.
Questions are never stupid, but answers sometimes can be. When you're learning something, you have to ask questions. When you've worked with something for a long time and understand it, some things seem obvious that are definitely not obvious to a beginner. So don't be shy about asking questions. That's how you learn.