943,981 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 81634
  • PHP RSS
Jan 22nd, 2005
0

embedding java script in php

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Jan 23rd, 2005
0

Re: embedding java script in php

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

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hise@ledcity.net is offline Offline
3 posts
since Jan 2005
Jan 23rd, 2005
0

Re: embedding java script in php

Hi,

i tried both of your examples but it didn't work. my php script stopped working altogether and i am presented with a blank white screen

Scoobie
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Jan 25th, 2005
0

Re: embedding java script in php

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Mar 9th, 2009
0

Re: embedding java script in php

you can use

header("Location: filename.php");

to send the person back to the form
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanFare is offline Offline
1 posts
since Mar 2009
Mar 13th, 2011
-1
Re: embedding java script in php
PHP Syntax (Toggle Plain Text)
  1. if (filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL))
  2. {
  3. $a=(filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL));
  4. echo $a;
  5. $errors[] = "$email is <strong>NOT</strong> a valid URL.";
  6. }
Last edited by Ezzaral; Mar 14th, 2011 at 4:01 pm. Reason: Added code tags. Please use them to format any code that you post, especially when dragging up years-old threads.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
durga@deepti is offline Offline
1 posts
since Mar 2011
May 12th, 2011
0
Re: embedding java script in php
When a user is sent with some info on url for example

http://localhost/integra/store/mater...hp?do=4&matid=

$action = $_GET[do];

and this code should go to the bottom of the page before the </body> so that all the data can be accessed.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. if($action==1)
  3. {
  4. print("<script language = 'javascript'>display_container('disp1')</script>");
  5. }
  6. if($action==2)
  7. {
  8. print("<script language = 'javascript'>display_container('disp2')</script>");
  9. }
  10. if($action==3)
  11. {
  12. print("<script language = 'javascript'>display_container('disp3')</script>");
  13. }
  14. if($action==4)
  15. {
  16. print("<script language = 'javascript'>display_container('disp4')</script>");
  17. }
  18.  
  19. ?>
Reputation Points: 10
Solved Threads: 0
Unverified User
shridharshenoy is offline Offline
1 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to use serverside validation in php
Next Thread in PHP Forum Timeline: Window is not open in new tab





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC