| | |
embedding java script in php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2005
Posts: 20
Reputation:
Solved Threads: 0
Hi,
i was wondering if anyone can help me. i am new to php and was wondering if it was possible to embed javascript in php. i was trying to get an alert/msg box up to explain an error to the user.
i have written the following code
<?php
.............
/* CHECK IF EMAIL ADDRESS SUBMITTED IS VALID */
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $UNameFrm))
{
<script type = "text/javascript">
alert('Congrats');
</script>
exit();
}
/* CHECK IF EMAIL ADDRESS ALREADY EXISTS IN THE DATABASE */
$check = mysql_query("select * from acctbl where Username = '$UNameFrm'");
$result = mysql_query($check);
$num = mysql_num_rows($check);
if ($num > 0)
{
echo "This email address already exists";
exit();
}
else
{
$insert = ......
}
?>
can anybody help me? if this can't be done with javascript can anybody tell me how to do it another way.
Thanks in Advanced,
Scoobie.
i was wondering if anyone can help me. i am new to php and was wondering if it was possible to embed javascript in php. i was trying to get an alert/msg box up to explain an error to the user.
i have written the following code
<?php
.............
/* CHECK IF EMAIL ADDRESS SUBMITTED IS VALID */
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $UNameFrm))
{
<script type = "text/javascript">
alert('Congrats');
</script>
exit();
}
/* CHECK IF EMAIL ADDRESS ALREADY EXISTS IN THE DATABASE */
$check = mysql_query("select * from acctbl where Username = '$UNameFrm'");
$result = mysql_query($check);
$num = mysql_num_rows($check);
if ($num > 0)
{
echo "This email address already exists";
exit();
}
else
{
$insert = ......
}
?>
can anybody help me? if this can't be done with javascript can anybody tell me how to do it another way.
Thanks in Advanced,
Scoobie.
Hi Scoobie:
It is very easy to embed Javascript in PHP. Remember that PHP code is executed on the server while JavaScript is executed by your browser. This means that javascript, like HTML, simply needs to be printed from PHP.
Below I've placed two examples of how you can change your PHP code to output the JavaScript pop-up.
Example 1:
----------------------------------------------------------------
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$UNameFrm))
{
?>
<script language = 'javascript'>
alert('Congrats');
</script>
<?
exit();
}
Example 2:
----------------------------------------------------------------
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $UNameFrm))
{
print("<script language = 'javascript'>alert('Congrats');</script>");
exit();
}
Please note that I've changed the "type = 'text/javascript'" to "language = 'javascript'" in both examples.
PHP and JavaScript work very well togehter. However, it's very easy to write a very confusing piece of code since their syntax is nearly identicle!
Good luck!
hise
It is very easy to embed Javascript in PHP. Remember that PHP code is executed on the server while JavaScript is executed by your browser. This means that javascript, like HTML, simply needs to be printed from PHP.
Below I've placed two examples of how you can change your PHP code to output the JavaScript pop-up.
Example 1:
----------------------------------------------------------------
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$UNameFrm))
{
?>
<script language = 'javascript'>
alert('Congrats');
</script>
<?
exit();
}
Example 2:
----------------------------------------------------------------
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $UNameFrm))
{
print("<script language = 'javascript'>alert('Congrats');</script>");
exit();
}
Please note that I've changed the "type = 'text/javascript'" to "language = 'javascript'" in both examples.
PHP and JavaScript work very well togehter. However, it's very easy to write a very confusing piece of code since their syntax is nearly identicle!

Good luck!
hise
•
•
•
•
Originally Posted by scoobie
Hi,
i was wondering if anyone can help me. i am new to php and was wondering if it was possible to embed javascript in php. i was trying to get an alert/msg box up to explain an error to the user.
i have written the following code
<?php
.............
/* CHECK IF EMAIL ADDRESS SUBMITTED IS VALID */
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $UNameFrm))
{
<script type = "text/javascript">
alert('Congrats');
</script>
exit();
}
/* CHECK IF EMAIL ADDRESS ALREADY EXISTS IN THE DATABASE */
$check = mysql_query("select * from acctbl where Username = '$UNameFrm'");
$result = mysql_query($check);
$num = mysql_num_rows($check);
if ($num > 0)
{
echo "This email address already exists";
exit();
}
else
{
$insert = ......
}
?>
can anybody help me? if this can't be done with javascript can anybody tell me how to do it another way.
Thanks in Advanced,
Scoobie.
•
•
Join Date: Jan 2005
Posts: 20
Reputation:
Solved Threads: 0
Hi,
i got it to work. i used the following code:
echo "<script type='text/javascript'>\n";
echo "alert('Congrats');\n";
echo "</script>";
the problem i have now is that it presents a blank white screen, how can i get it to go back to the screen so that they can reenter the info after they click ok on the alert msg box
scoobie
i got it to work. i used the following code:
echo "<script type='text/javascript'>\n";
echo "alert('Congrats');\n";
echo "</script>";
the problem i have now is that it presents a blank white screen, how can i get it to go back to the screen so that they can reenter the info after they click ok on the alert msg box
scoobie
![]() |
Similar Threads
- Free PHP , JAVA SCRIPT , CSS , HTML and QTP tutorials ( study material ) (Website Reviews)
- Free PHP , JAVA SCRIPT , CSS , HTML and QTP tutorials ( study material ) (Website Reviews)
- Free PHP , JAVA SCRIPT , CSS , HTML and QTP tutorials ( study material ) (Website Reviews)
- Free PHP , JAVA SCRIPT , CSS , HTML and QTP tutorials ( study material ) (Website Reviews)
Other Threads in the PHP Forum
- Previous Thread: Check out the code ...
- Next Thread: PHP page loading
| Thread Tools | Search this Thread |
# 5.2.10 access action address apache api array auto autoincrement broken cakephp checkbox class classes clean clients cms code cron curl database date dehasher destroy directory dissertation domain dropdown dynamic echo$_get[x]changingitintovariable... email encode error errorlog fairness fatalerror file folder form function functions href htaccess html image include indentedsubcategory ip javascript joomla legislation limit link load local login mail masterthesis memberships menu methods multiple multipletables mysql mysqlquery newsletters oop open passwords paypal pdf persist php popup provider query radio random script search secure server sessions simple sockets source space spam sql system table tutorial upload url user variable voteup web youtube





