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
Header("content-type: application/x-javascript");
echo “document.write(‘Hello World I am php code called from HTML file:’);”;
The test.html code is
“<script language="JavaScript" type="text/javascript" src="http://localhost/testhtml/test.php"></script>”
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

Recommended Answers

All 13 Replies

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

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

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

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.

something like

<form action='submit_me.php' method='post' onsubmit='validate_javascript();'>
bla bla bla
<input type='submit'></form>
Member Avatar
<script language="JavaScript" type="text/javascript">

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">

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.

Member Avatar

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.

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

CLOSE WINDOW; is not a command.

Thanks once again RajaRajan. R for the help.
"My PHP file code is
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
<h1> HELLO TESTING </h1>

<script language="JavaScript" type="text/javascript">

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"


Member Avatar
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.

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
// call php

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

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

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

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


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.