
07-16-07, 09:01 PM
|
 |
Community VIP
|
|
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
job0107, here is a test that will prove to you that the PHP code on the page is executed when the page is requested and not as the result of the execution of the javascript -
Reverse the order of the two alert statements, so that the one containing the PHP "function call" is last.
Browse to the page and let the first alert statement execute and display the "You just created a file..." (ignoring what it is saying.)
Examine the web server folder and note that the .txt file has been created. You can also view the source of the page in the browser at this point and note that it already contains the output from the echo statement in the php function code, not a reference to the PHP function call.
Click the OK button on the first alert.
Click the OK button on the second alert (with the PHP "function call" in it.)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
|

07-17-07, 05:50 AM
|
 |
Coding Addict
|
|
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
|
|
if wanting to echo vars in html javascript and other scripts you can always use:
<?=$phpvar?> alot shorter than <?php echo $phpvar ?>
|

07-17-07, 07:37 AM
|
|
Newbie Coder
|
|
Join Date: Nov 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hmm. Ok makes sense then. So I guess the next step here is to learn more about httprequests. Say the client loads the page. I have a form that processes data. When the form is submitted, I want to call an httprequest to grab a php script. The script will run in the background I assume?
Can that php script acess the page display?(say it was doing a calculation and brining back the result into the form.)
Then pass the variables and use javascript to update it via innerHTML?
what I'm ultimately trying to achieve is a form that uses PHP to grab info from a database, print it into a table, the user select various options, then PHP runs calculations and updates the data Without a page reload. Then the new figures are saved to the database.
any sites that anyone knows of that has examples of http requests used with php?
|

07-17-07, 08:05 AM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Quote:
The above examples won't work as you might expect them to.
All the PHP code will execute on the server before the page is sent to the client, meaning all of the PHP calls have all been replaced by whatever text they returned once the JavaScript is able to execute.
JavaScript Code:
<script>
document.write("<?php echo 'hello!'; ?>");
</script>
will look like
JavaScript Code:
<script>
document.write("hello!");
</script>
when received by the client, so the browser won't ever know PHP has been involved at all.
The next piece of code would look like this:
JavaScript Code:
<!-- PHP Section -->
<!-- HTML Section -->
<button onclick="msg1()">MESSAGE 1</button>
<button onclick="msg2()">MESSAGE 2</button>
<button onclick="msg3()">MESSAGE 3</button>
<button onclick="msg4()">MESSAGE 4</button>
<button onclick="alert('You pressed the MESSAGE 5 button')">MESSAGE 5</button>
<!-- Javascript Section -->
<script>
function msg1(){alert("You pressed the MESSAGE 1 button");}
function msg2(){alert("You pressed the MESSAGE 2 button");}
function msg3(){alert("You pressed the MESSAGE 3 button");}
function msg4(){alert("You pressed the MESSAGE 4 button");}
</script>
The last example doesn't do much good since it creates the file BEFORE the page is sent to the client and any JavaScript execution can occurr. The file was indeed created and the message saying this will be displayed, but it all happened before the browser knew anything about it.
To the browser, it just looks like this:
JavaScript Code:
<script>
alert("Saving data to the file");
alert("You just created a file called \"test_data_write.txt\".\nYou could have as easily, written data to a database.");
</script>
|
Quote:
I'm not sure you understood what the code job0107 posted does, but most importantly, what it doesn't. It gives the appearance of PHP code on the server being executed by JavaScript on the client, which is impossible.
The serverside PHP code has already been executed by the server before ever reaching the client. And since the client isn't allowed to see servercode, it's never even sent there, hence the JavaScript cannot call it. The browser wouldn't understand the PHP code anyway since only servers need to do that.
Likewise, the server cannot use the JavaScript code meant for the browser, since it all runs on the client without any open connections. If you want to send a JavaScript variable to PHP, you must either send it as POST or GET data in a HTTP request.
You can ONLY "call" serverside code from the client indirectly, with a HTTP request for a new page. The PHP code will then execute and return something new, which the clientside code should know how to handle.
----
How secure AJAX is depends on the implementation and how it's used.
It's no more or less secure than having a user submitting data via a form or a querystring and then using that data on the server, since that's more or less exactly what happens.
Nothing really changes security-wise by using AJAX, you still have to be careful not to use any data sent by the client without first scanning it for malicious code (esp. when dealing with file or database access). Never trust the client, no matter what the interface on that end looks like, since you can't ever be sure they even used that interface when submitting the data.
|
Quote:
No, you don't get the point.
JavaScript doesn't call the create_file() function.
It is called on the server BEFORE the page is sent to the client.
The SERVER replaces the function call with the text "Saving data to the file".
The browser and JavaScript interpreter can only see this:
JavaScript Code:
alert("Saving data to the file");
The code would do EXACTLY the same thing if you did:
php Code:
<?php
function create_file ()
{
$a = fopen("test_data_write_.txt", "w");
fwrite($a, "Hello World.\r\n");
fclose($a);
echo "Saving data to the file"; }
?>
<?PHP create_file () ?> <!-- This function call is executed and replaced by the returned text as soon as the server encounters it. -->
No JavaScript can ever call PHP code on the server, because the browser doesn't know the PHP code exists, nor could it ever get access to it.
If you don't believe me, try removing all JavaScript code in that example and it will still work.
|
Quote:
job0107, here is a test that will prove to you that the PHP code on the page is executed when the page is requested and not as the result of the execution of the javascript -
Reverse the order of the two alert statements, so that the one containing the PHP "function call" is last.
Browse to the page and let the first alert statement execute and display the "You just created a file..." (ignoring what it is saying.)
Examine the web server folder and note that the .txt file has been created. You can also view the source of the page in the browser at this point and note that it already contains the output from the echo statement in the php function code, not a reference to the PHP function call.
Click the OK button on the first alert.
Click the OK button on the second alert (with the PHP "function call" in it.)
|
All of the above are correct, everything that is happening is done from the server before the page is sent to the client. If you were to try to update the file that was created in the previous example, Javascript would either have to submit a form or create a new ActiveXObject() to handle the request.
Quote:
Internet Explorer on Windows, Safari on Mac OS-X, Mozilla on all platforms, Konqueror in KDE, IceBrowser on Java, and Opera on all platforms including Symbian provide a method for client side javascript to make HTTP requests. From the humble begins as an oddly named object with few admirers, it's blossomed to be the core technology in something called AJAX .
The Object makes many things easier and neater than they otherwise would be, and introduces some things that were otherwise impossible such as HEAD requests to see when a resource was last modified, or to see if it even exists. It makes your scripting options more flexible allowing for POST requests without having the page change, and opens up the possibility of using PUT, DELETE etc. These methods are increasingly used to provide richer Web Applications like G-Mail that use lower bandwidth and offer snappier user interaction.
Why XML HTTP Request object?
Whilst the object is called the XML HTTP Request object it is not limited to being used with XML, it can request or send any type of document, although dealing with binary streams can be problematical in javascript.
Creating the object
In Internet Explorer, you create the object using new ActiveXObject("Msxml2.XMLHTTP") or new ActiveXObject("Microsoft.XMLHTTP") depending on the version of MSXML installed. In Mozilla and Safari (and likely in future UA's that support it) you use new XMLHttpRequest() IceBrowser uses yet another method the window.createRequest() method.
|
__________________
Jerry Broughton
|

03-05-10, 12:43 AM
|
|
New Member
|
|
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
I am not sure if this it what will like to do ...
<script type='text/javascript'>
if(typeof jQuery != 'function'){
</script>
<?php
echo "<script type='text/javascript' src='../js/jquery-1.3.2.js'></script>";
?>
<script type='text/javascript'>
}
else{
}
</script>
Hope it helps...
|

03-05-10, 02:05 AM
|
|
New Member
|
|
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
I am sorry the last example doesn't work,
I am sorry the last example doesn't work, if you want to pass a variable from javascript to PHP, you need to write everything on PHP, then pass the javascript variable with >> window.location.href , then get it with PHP.
I am sorry the last example doesn't work,
<?php
if($_GET['lemonk_include']=="0"){
echo ("<script type='text/javascript' src='../js/jquery-1.3.2.js'></script>");
}
else{
echo ("<script language='javascript' type='text/javascript'>");
echo ("if(typeof jQuery != 'function'){window.location.href = location.href + '&lemonk_include=' + '0';}else{}");
echo ("</script");
}
?>
This is also for detecting if JQuery was detected, if not, it will be included once.
|

07-01-10, 10:19 PM
|
|
New Member
|
|
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
What about this?
Hi,
What about something like this? Will this work?
|

07-05-10, 06:48 AM
|
 |
Newbie Coder
|
|
Join Date: Jun 2010
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes it have to work if your url is correct. The PHP Variable works fine in Java Script.
|

07-09-10, 02:05 AM
|
 |
Newbie Coder
|
|
Join Date: Jul 2010
Location: UK
Posts: 45
Thanks: 1
Thanked 1 Time in 1 Post
|
|
you can pass variables from javascript to php and from php to javascript, but in both cases you have to have php installed with a server.
|
|
Currently Active Users Viewing This Thread: 5 (0 members and 5 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|