1.11M Members

how to call php file using external javascript

 
0
 

the error i am getting in the html file
Message: Syntax error
Line: 1
Char: 1
Code: 0
URI: http://localhost/testhtml/test.php
The test.php code is
<?php
Header("content-type: application/x-javascript");
echo “document.write(‘Hello World I am php code called from HTML file:’);”;
CLOSE WINDOW;
?>
The test.html code is
“<script language="JavaScript" type="text/javascript" src="http://localhost/testhtml/test.php"></script>”
HELLO TESTING
i have enclosed external javascript call within single quote so that it gets displayed here
Can anyone help me in resolving this. I am new to php

 
0
 

The error is probably becuase the functions are case sensitive and you have capitalized "H".

 
0
 

that is an unneccessarily convoluted way to echo text, what do you actually want to achieve

 
0
 

i never tried including a php file using javascript external, if you really want to include a php file user php include() or require() instead

 
0
 

thank you so much for your responses.
As i said i am new to php.
can you please relate an example code, of using include() or require().
I searched through some materials and came to know the difference between include() and require() is
the latter stops processing incase any errors but former continues.

My requirement is simple.
I have a contact us page along with other pages all designed in html. I am validating all fields of contact us page using javascript. If values are in correct format i am calling the php file for sending mails.
My js script is working perfectly. the only thing i need to learn is how to call and execute that php file from javascript so that i can send mails.

Your help is highly appreciated.
Pankaj

 
0
 

something like

<form action='submit_me.php' method='post' onsubmit='validate_javascript();'>
bla bla bla
<input type='submit'></form>
 
0
 
<script language="JavaScript" type="text/javascript">
	window.location="http://localhost/testhtml/test.php";
</script>
 
0
 

Thank you RajaRajan. R for the help.
<script language="JavaScript" type="text/javascript"> window.location="http://localhost/testhtml/test.php";</script><script language="JavaScript" type="text/javascript">
window.location="http://localhost/testhtml/test.php";
</script>

does call the desired php file.
But it is displaying
echo "document.write('Hello World I am php code called from HTML file:');"; CLOSE WINDOW; ?>

which i guess is not processing the php file.
I think this echo command should not be displayed bec it's a command and not plain text if the php file is processed correctly.

 
0
 

What you want to do with your test.php? Now is working your test.php just print what you have feed in your php file. Just change as below to display text.

<?php
echo "Hello World I am php code called from HTML file";
?>

CLOSE WINDOW; is not a command.

 
0
 

Thanks once again RajaRajan. R for the help.
"My PHP file code is
"<?php
header("content-type: application/x-javascript");
<script type="text/javascript" src="http://localhost/testhtml/test.php"> </script>
echo "Hello World I am php code called from HTML file ";
?>"

My HTML file code is
"<html>
<head>
<h1> HELLO TESTING </h1>
</head>
<body>
</body>
</html>

<script language="JavaScript" type="text/javascript">
window.location="http://localhost/testhtml/test.php";
</script>"

Here i have placed the " so that they get displayed here

The display after changes is
echo "Hello World I am php code called from HTML file "; ?>


Now i have a doubt that is the file is processed properly why does it displays echo command.
I think the display should have been
Hello World I am php code called from HTML file

I will be obliged if you can tell me how to return back to the html file after php code execution is completed. An example will be of great help"

Pankaj

 
0
 
<?php
echo "Hello World I am php code called from HTML file ";
echo "<a href='html path'>click here to return</a>";
?>

Use simply like above in ur php file. Have a link there to return back.

 
0
 

first understand the difference about server-side and client-side scripting, server-side request data from server and reloads the page or not if you use ajax, and client side does not need the page to refresh before you can use it, i hope i understand you correctly, you want to use javascript which is a client-side to request data from php which is server-side, like

<script type="text/javascript">
function form_validation(){ 
if(validation==true){ // sample, if validation is true
<?php
// call php
?>
}
}
</script>

that's impossible, coz in order for you to process php you need to request to the server first commonly via form submit and then the page reloads, unless you use ajax, but php can

<?php
if($_GET['sampe']){ // sample
echo "<script>alert('you pressed it')</script>"; // call javascript or even html
}else{
echo "<script>alert('no you dont')</script>";
}
?>
 
-1
 

what you are asking for is not what you are attempting
you asked for a form processed by a php form handler after it has been validated by a javascript ?
wrong way to do things
javascript validation isnt validation, it can be faked wrong blocked disabled
the form should submit to the form handler to be validated, basic javascript validation can be used, prior to submission, but its no value for real work
if validation fails, return to the form for correction

<form method='post' action='phpscripthandler'>
<input name='ralph' value='<?php if isset($_GET['ralph'] echo $_GET['ralph']; ?>'><label for='ralph'> ralph</label>
bla bla bla

example code ralph is populated with return values from the handler script to be edited for resubmission

 
0
 

Thank You guys for all the help. I resolved this problem using asp.

Pankaj

Question Answered as of 4 Years Ago by rajarajan07, almostbob, vaultdweller123 and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article