Forum Moderators: open

Message Too Old, No Replies

Call external php with Javascript

         

andrewsmd

7:37 pm on Jun 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to call a file called test.php with javascript when a user clicks on a button. I have checkboxes and I don't want to loose their value so I want the javascript to run the php code. Any suggestions.

WesleyC

9:43 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



<script type="text/javascript">
function doAction()
{
var target = document.createElement( "script" );
target.setAttribute( "src", "http://example.com/my_php_page.php?rand=some_unique_string_to_prevent_caching" );
document.getElementsByTagName( "body" )[0].appendChild( target );
}
</script>

Then, in your button's click event, just call doAction().

Alternately, you could use AJAX--but this is probably somewhat simpler of a solution until you start needing the PHP page in question to return a value of some sort. In that case, just use the YUI connection manager or something of that nature. :)

andrewsmd

1:40 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok here is my deal. I have an external PHP file that looks in a source folder. If there are files it will move one, then open it and search for a specific string. These strings are unique ID numbers. Then the PHP file edits another text file inserting the ID number associated with the original file in a specific place. Then it displays the contents of the text file as output. I have been implementing this with a link that just takes the user to a different page because the page the user comes from has checkboxes and I don't want to loose their value. As php runs on the server if I run that file on a button click we loose all of the checkboxes that have already been checked. They need to stay checked for the user to submit at the end. Here is step by step of how my program works

andrewsmd

1:46 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The user gets to the point in the form and clicks on the Link. The link opens a new window with a new page with two buttons. One that says edit script and one that says archive file. Also, at the top of the page the number of files left to send is displayed. For simplicity sake we will say there are two files. temp1.txt and temp2.txt. When the user clicks the edit script button the PHP file moves temp1.txt to a sending folder, then opens it and stores the txt file into an array (or hash table since php doesn't actually have arrays). Then it searches for a specific string based on a regex. Lets say this string is ab123. Then the file opens a specific text file and goes to a specific place in the file and replaces the string already there with ab123. It also creates a copy of the edited text file in an archive folder. Then the php file outputs the text file to the html page for the user to view. Once the user does some other things to send the file, they click the archive button and another php file moves the file to an archive folder from the sending. What I want is to display that php output in an alert box with js without loosing my checkbox values. Normally I would use templates and a session to store the values of a checkbox, but we do not have PEAR at work and I cannot edit our php server. Sorry the post is so long and complicated. Any suggestions, thanks in advance.

WesleyC

4:18 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



For something this complex you'll need to either use an iFrame or AJAX--probably AJAX. The code for this depends on what implementation you choose.

andrewsmd

5:16 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually I found a work around. In my php code I can just set a header content type. I have ran into a snag with outputting multiple lines, but it works. Here is the code.
Header("content-type: application/x-javascript");

//displays an alert box with a php varible $msg passed into it
function echoAlert($msg){

echo "alert('$msg')";

}//echoAlert
//now just run whatever code we want here is some example code.

$var = "hello";

if($var == "hello"){

echoAlert("true");

}//if
else{
echoAlert("false");
}//else

that will give you an alert box with true. change $var and you get false.
This is actually really handy because you can get the power of php and run it almost as if you are executing it from the client side