Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking values

Status
Not open for further replies.

HVtech

IS-IT--Management
Jul 1, 2010
71
NL
Hi,
I'm trying to get this working but no luck so far..
I'm trying to check a few input fields on a form.
This is the code I have:
Code:
  <SCRIPT language="JavaScript">
    function validate(form) {
      if (form.name.value=="") {
        alert("Vul je naam in");
        return false;
      } if (form.message.value=="") {
        alert("Vul je bericht in");
        return false;
      } if (! (form.url.value=="")) {
        alert("Geen URL ingevuld");
        return false;
    }
                </SCRIPT>
When name or message have no value, output should come
When url is filled, output should come.

 
Can we see your form?
And how you are calling this code?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
This all I have..
Code:
 <?
  $amount=15;

  if ($_GET['action']=='write') {
      header('location: gasten.php?action=read&start=0');
    $file=fopen('gastenboek.txt','a');

    $message=str_replace("\r",'',$message);
    $message=str_replace("\n",'{{',$_GET['message']);
    fwrite($file,$_GET['name'].'|||'.$_GET['email'].'|||'.date('d-m-Y').'|||'.$_GET['url'].'|||'.$message."\n");
    fclose($file);
    
  }

  $start=(isset($_GET['start'])?$_GET['start']:0);
  $gastenboek=Array();
  $gastenboek=file('gastenboek.txt');

?>

                <SCRIPT language="JavaScript">
    function validate(form) {
      if (form.name.value=="") {
        alert("Vul je naam in");
        return false;
      } if (form.message.value=="") {
        alert("Vul je bericht in");
        return false;
      } if (! (form.url.value=="")) {
        alert("Geen URL ingevuld");
        return false;
    }
                </SCRIPT>
              </p>
<TABLE width="100%" cellspacing="0" cellpadding="0">
  <TR>
      <TD class="side"> Berichten <? echo $start+1; ?> tot en met <? echo min($start+$amount,sizeof($gastenboek)); ?>.
        <HR> <TABLE width="100%" >
          <?
          $gastenboek=array_reverse($gastenboek);
          for ($i=$start;$i<$start+$amount && $i<sizeof($gastenboek);$i++) {
            list($name,$email,$date,$url,$message)=explode('|||',$gastenboek[$i]);
            $message=str_replace('{{',"\n",$message);
            echo '<TR><TD><B>'.($email!=""?'<A href="mailto:'.$email.'">'.$name.'</A>':$name).'</B></TD><TD align="right"><B>'.$date.'</B></TD></TR>'."\n";
            echo ($url!=""?'<TR><TD colspan="2"><A href="'.$url.'" target="_blank">'.$url.'</A></TD></TR>':'')."\n";
            echo '<TR><TD colspan="2"><SPAN>'.str_replace("\n",'<BR>',htmlspecialchars($message)).'</SPAN></TD></TR>'."\n";
            echo '<TR><TD colspan="2"><HR></TD></TR>'."\n";
          }
        ?>
        </TABLE>
        <CENTER>
          <?
          if ($start>0) echo '<A href="gasten.php?start='.max(0,$start-$amount).'"><<<</A> ';
          if ($start+$amount<sizeof($gastenboek)) echo ' <A href="gasten.php?start='.($start+$amount).'">>>></A>';
        ?>   </CENTER></TD>
    </TR>
</TABLE>
  <TABLE width="100%" cellspacing="0" cellpadding="0" >
    <TR>
      <TD class="side">Nieuw bericht</TD>
    </TR>
  </TABLE>
  <TABLE width="100%" cellspacing="0" cellpadding="0" >
    <TR>
      <TD class="side"> <FORM action="gasten.php" method="GET" onSubmit="return validate(this);">
          <INPUT type="hidden" name="action" value="write">
          <TABLE class="side">
            <TR>
              <TD>Naam:</TD>
              <TD><INPUT type="text" name="name" size="30"></TD>
            </TR>
            <TR>
              <TD>E-Mail:</TD>
              <TD><INPUT type="text" name="email" size="30">
                (optioneel)</TD>
            </TR>
            <TR>
              <TD>Homepage:</TD>
              <TD><INPUT name="url" type="text" value="" size="30">
                (optioneel)</TD>
            </TR>
            <TR>
              <TD>Bericht:</TD>
              <TD><TEXTAREA name="message" cols="50" rows="5"></TEXTAREA></TD>
            </TR>
          </TABLE>
          <INPUT type="submit" value="Verstuur">
        </FORM></TD>
    </TR>
  </TABLE>
 
Almost forgot:
When the script works I want to add this
<style type="text/css">
.antispam { display:none;}
</style>

And give the url field

<p class="antispam">Leave this empty:
<br /><input name="url" /></p>

So a normal user doesn't see the url field, but spambots will probably fill it and get nothing posted :)
 
Your function is missing a closing " } " at the end. This would be generating an error.

Code:
function validate(form) [red]{[/red]
      if (form.name.value=="") [blue]{[/blue]
        alert("Vul je naam in");
        return false;
      [blue]}[/blue] if (form.message.value=="") [green]{[/green]
        alert("Vul je bericht in");
        return false;
      [green]}[/green] if (! (form.url.value=="")) [COLOR=black yellow]{[/color]
        alert("Geen URL ingevuld");
        return false;
   [COLOR=black yellow]}[/color]

[red]>><<[/red]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for your support, but the form is not working correct.
I just tested it, entered a url and the message was posted which is not supposes to with a url entered.
Do you have any ideas ?
 
It works for me. I can see nothing else wrong. Make sure you are getting no errors.

If you are using IE look at the yellow shield at the bottom left core of the window. Double click it to show any errors.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Driving me nuts!! I've struggling for a couples of hours now, thought I had it..
In IE I do see an error about missing a '}' on line 56 which is the end of the script. If I put the } there, the code does not run..
This is the part where the } is missing according to IE:
Code:
 <?php
  $amount=15;

  if ($_GET['action']=='write') {
      header('location: gasten.php?action=read&start=0');
    $file=fopen('gastenboek.txt','a');

    $message=str_replace("\r",'',$message);
    $message=str_replace("\n",'{{',$_GET['message']);
    fwrite($file,$_GET['name'].'|||'.$_GET['email'].'|||'.date('d-m-Y').'|||'.$_GET['url'].'|||'.$message."\n");
    fclose($file);
    
  } //This is line 57 in the script

  $start=(isset($_GET['start'])?$_GET['start']:0);
  $gastenboek=Array();
  $gastenboek=file('gastenboek.txt');

?>


I changed the javascript a little too, becuase it doesn't work how it's ment.
Now I have this:
Code:
                <SCRIPT language="JavaScript">
    function validate(form) {
      if (form.name.value=="") {
        alert("Vul je naam in");
        return false;
      } else if (form.message.value=="") {
        alert("Vul je bericht in");
        return false;
      } else if (!(form.url.value="")) {
        return false;
      }
    }
                </SCRIPT>
I don't show the field URL to user by giveing it a css class
<style type="text/css">
.antispam { display:none;}
</style>

The idea is that spambots will fill in an URL and are recognized as bots (no one else sees the URL field..)

I believe the javascript is working but no message is posted now.
Can anyone please help me out ??
Thanks a lot!
 
Oke got it working now !
Done it like this:
Code:
  <SCRIPT language="JavaScript">
    function validate(form) 
  {
      if (form.name.value=="") 
	{
      alert("Vul je naam in");
      return false;
    } 
	else if (form.message.value=="") 
	{
      alert("Vul je bericht in");
      return false;
    } 
	else if (form.url.value=="") 
	{
	  return true;
	} 
	else 
	{
	 return false;
	}
  }
 </SCRIPT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top