JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 20th, 2000, 09:41 AM
stephanie stephanie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 8 stephanie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I need to test for a value in PHP (that is the easy part). If the value is null I would like to call a JavaScript function that will display an alert box. Or, I could always display an alert box directly from my PHP script should PHP allow for this.

Please help either on syntax for call to JavaScript or PHP error handling...

Thanks,

Stephanie

Reply With Quote
  #2  
Old June 20th, 2000, 02:06 PM
Ethereal Ethereal is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 1 Ethereal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Stephanie,

The Kind of COde you may be looking for is:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><HTML>
<HEAD>
<SCRIPT LANGUAGE="javascript"><!--
function ProcA () {
alert ("message");
}
// --></SCRIPT>
...
<?

if ($value eq "") {
echo "<SCRIPT LANGUAGE="javascript"><!--n";
echo "ProcA();n";
echo "// --></SCRIPT>n";
}

?>
...
</HTML>[/code]

Or simply:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><HTML>
<HEAD>
<?

if ($value eq "") {
echo "<SCRIPT LANGUAGE="javascript"><!--n";
echo " alert ("message");n";
echo "// --></SCRIPT>n";
}

?>
...
</HTML>[/code]

HTH,

Ethereal

Reply With Quote
  #3  
Old June 20th, 2000, 02:25 PM
stephanie stephanie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 8 stephanie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the help it is appreciated....

This is what worked for me...
<?php
if ($ID = '') {
echo "<script language=javascript>alert('Please enter a valid username.')</script>";
}
?>

Reply With Quote
  #4  
Old June 20th, 2000, 10:32 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,382 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 10 h 44 m 43 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Number 1:
Why use javascript? pop up alert boxes are annoying. that's the beauty of php, you can have the same code create different looks for a page, based on the values of certain variables.

If something in a form wasn't filled in, show the form again, with the text colored red around it. functions work very well for this... If you have a text box called text1, then echo it's value as: echo "value=$text1"; That way if you process the form and there are errors, you can show the form again, and their original values will still be filled in. if it's the first visit to the page, $text1 will be empty, so you'll just get an empty text box...

Number 2:
Why use php? You can do most of your validating before you even submit the form, on the client side. if you have a form like this:
<form method=post action=mypage.php3 name="my_form" onsubmit="validate();">
<input type=text name="userid">
<input type=submit value="Submit">
</form>

you can use the javascript function to deny submitting the form if the values aren't filled in:
<script language="javascript">
if (window.document.my_form.userid.value = '')
{ return false; }
else
{ return true; }
</script>

the function won't submit the form if the text box doesn't have anything in it..

I know this is very lengthy, but hopefully it gives you a few options or ideas...

Email me if you want some more code examples...

---John Holmes
---john.holmes@mindspring.com

Reply With Quote
  #5  
Old June 21st, 2000, 07:04 AM
stephanie stephanie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Posts: 8 stephanie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you for all the help it is much appreciated.

My web application is a little different than what has been explained here. It is basically a web-based electronic in/out board (this part of it anyhow). A user only has to log on once which is validated on the client side. That is no problem. Once they log on successfully a cookie is set to their machine to remember their UserID. My problem was that sometimes cookies get deleted and I had no validation to check whether it actually had a UserID or not. So when the page loads I need to check for a null or invalid UserID. There is no user input and no buttons clicked to invoke any procedures.

My solution (yes I actually figured it out!!) was similar to this...
<?php
if ($UserID == '') { //plus testing for wrong values...
echo "<scriptlanguage=javascript>alert('Please logon again to reset the cookie!')</script>";
}
?>

It is not the most elaborate code one has seen but it works for me!
Thanks again for your help!

Stephanie

Reply With Quote
  #6  
Old August 25th, 2008, 05:29 PM
peteheller peteheller is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Location: Northern California
Posts: 1 peteheller User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 55 sec
Reputation Power: 0
John,

Thanks for this very useful information regardding validating input from forms. Could you (or others with ideas) provide more guidance/code regarding this?

Javascript followup question:
<form method=post action=mypage.php3 name="my_form" onsubmit="validate();">

How is the validate() associated with the javascript code? Won't I need to create a function named validate?. E.g.,

<script language="javascript">
function validate() {
if (window.document.my_form.userid.value = '')
{ return false; }
else
{ return true; }
}
</script>

PHP followup question: I don't know how to (re)show the same form again if there are user input errors. Do you have a code example for this? I think I'd like to put the form inside a while statement until valid values are provided. Do you recommend this approach? And to indicate to the user that the form has changed since they submitted it, I'd like to set the background color to red for the input box for which the invalid values were submitted and to add a message to "Please reenter XXXX value" next to the text input field. Do you have a PHP code examples for doing that?

Thanks very much!
PeteH

Reply With Quote
  #7  
Old August 26th, 2008, 06:23 AM
addylad addylad is offline
Room for advertising!
Click here for more information.
 
Join Date: Nov 2006
Location: Here.
Posts: 306 addylad User rank is Corporal (100 - 500 Reputation Level)addylad User rank is Corporal (100 - 500 Reputation Level)addylad User rank is Corporal (100 - 500 Reputation Level)addylad User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 30 sec
Reputation Power: 5
Send a message via MSN to addylad
To show the form based on POST:
if(!isset($_POST)) {
// show form
}else{
// add to database and show message to confirm
}

Instead of while() loops you could use the onChange event to see whether the info has changed I believe.
Comments on this post
mikeyskona agrees: About time you got a little rep for being helpful
__________________
Give me reputation if I'm helpful, use the scales icon, top right-hand corner (). Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Calling JavaScript function from PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway