I am having trouble with a form, help please!

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
I have written a script that you fill in a form and then submit it but am having a little trouble with it. Also need feeback and sugestions if you wouldn't mind.
http://www.defectalisman.com/examples/form.php

It uses basic php, html & javascript.
It submits to $PHP_SELF.

The prob I am having is that if you refresh the page and then fill in all the requiered feilds (put a return at the end of the comments box ) and press enter on another feild it skips the javascript and there after (without a refresh) even pressing the preveiw button does the same?

This is the code for the script.
PHP:
<?php
// Change to 1 to make it test itself
$test = 0;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Mail Form - DefecTalisman</title>
<style type="text/css">
<!--
.style1 {font-size: 9px}
-->
</style>
<script type="text/javascript" language="javascript" src="req.js">
</script>
</head>
<?php
if (!isset($_POST['preview']))
{
?>
<body>
<p align="center">Basic Form that checks itself. </p>
<form method="post" action="<?php echo $PHP_SELF; ?>">
  <table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
<td>Your Name  :</td>
    <td><input type="text" size="50" maxlength="100" name="usr_name" /> 
      * </td>
  </tr>
 
  <tr>
    <td>Your e-mail Address : </td>
    <td><input type="text" size="50" maxlength="100" name="usr_mail" />
*</td>
  </tr>
 
  <tr>
    <td>Subject : </td>
    <td><input type="text" size="50" maxlength="100" name="subject" />
*</td>
  </tr>
 
    <tr>
      <td>Type Of Message :      </td>
      <td><select name="msg_type">
        <option value='' ="selected">Please Select A Catagory</option>
        <option value="support">Support</option>
        <option value="c_care">Customer Care</option>
      </select>
*</td>
    </tr>
 
 
    <tr>
    <td colspan="2"><div align="center">Comments *
      <br />
      <textarea rows="5" cols="50" name="comments" wrap="physical"></textarea>
    </div></td>
    </tr>
  <tr>
    <td>Other : </td>
    <td><input type="text" size="50" maxlength="100" name="other" /></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
   <input type="submit" value="Preview" name="preview" onKeyPress="history.go(-1)"/>
      <input type="reset" value="Default" />
      <input type ="button" value="Back" name ="back" onClick="history.go(-1)" />
    </div></td>
    </tr>
</table>
</form>
</body>
<?php
}
?>
<?php
/*
 * This is where the form gets procesed if the preview button was pressed
 */
if (isset($_POST['preview']))
{
 // Enter the required feilds into the array along with their scrren names
 $reqired_fields = array(
       'usr_name' => 'Your Name',
       'usr_mail' => 'Your e-mail Address',
       'subject' => 'Subject',
       'msg_type' => 'Type Of Message',
       'comments' => 'Comments'
       );
 $null_fields = array();
 
 // This checks the feilds and then collects their screen names
 foreach ($reqired_fields as $a => $z)
 {
  if (isset($_POST["$a"]))
  {
   if ($_POST[$a] == FALSE) { array_push($null_fields,$z); }
  }
 }
 
 // If there are missing feilds then pop up and alert which ones, then go back 1
 if ($null_fields == TRUE)
 {
// BREAK for JavaScript
?>
<script language="JavaScript" type="text/javascript">
reqPopUP("<?php foreach ($null_fields as $t) { echo '\n*'.$t ; } ?>");
</script>
<?php
 }
 
 // If the required feilds are present
 if ($null_fields == FALSE)
 {
  // Set them to a scalars
  $to = 'defectalisman@hotmail.com';
  $usr_name = $_POST['usr_name'];
  $usr_add = $_POST['usr_mail'];
  $type = $_POST['msg_type'];
  $sub = $_POST['subject'];
  $msg = $_POST['comments'];
 
  // Set any other feilds from the form to scalars
  if ($_POST['other'] == TRUE) { $other = $_POST['other']; } else { $other = 'Nothing'; }
 
  // Set an array for pop up
  $form_output = array();
  array_push($form_output, $usr_name, $usr_add, $type, $sub, $msg, $other);
// BREAK for JavaScript
?>
<script language="JavaScript" type="text/javascript">
goodPopUP("<?php foreach ($form_output as $t) { echo '\n-'.$t ; } ?>");
</script>
<?php
  // Now here we can work with the data (maybe call a php function)
  echo 'All seems fine now its got to do something with the data.<br>';
  echo '<input type ="button" value="Back" name ="back" onclick="history.go(-2)" />';
 }
}
?>
</form>
</body>
</html>

Like I said any feedback as a better way to do something please post.

P.S. Also it seems to take a while to process the form ? ? ?
 
Last edited:

mitamata

New Member
Messages
81
Reaction score
0
Points
0
The reason it skips the javascript is because there's an error in it. It's in this part:
Code:
<script language="JavaScript" type="text/javascript">
goodPopUP("<?php foreach ($form_output as $t) { echo '\n-'.$t ; } ?>");
</script>
I filled the form with some data and this failed to execute:
Code:
<script language="JavaScript" type="text/javascript">
goodPopUP("\n-Manca\n-test@gmail.com\n-c_care\n-New message\n-Testing
A message\n-Yes");
</script>
The error firebug reported was "unterminated string literal". The problem is the line break in the text area. If I try a message that has only one line, it works fine. So you need to find a way to handle the line break.
I knew how to do that at some point, but I don't have time to look in to it at the moment cause I'm already late for something. I'll be back later/tomorrow with more input. Good luck!

Btw, do you use firebug (it's a plugin for firefox)? I can't imagine coding without it anymore.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
even when I use:

PHP:
		if (strpos($msg,'\n') == TRUE) { $msg = str_replace('\n',"   ",$msg); }
		if (strpos($msg,'\r') == TRUE) { $msg = str_replace('\r',"   ",$msg); }
		if (strpos($msg,'<br>') == TRUE) { $msg = str_replace('<br>',"   ",$msg); }

on the comments input it does the same thing?
Edit:
or this also doesn't help:

PHP:
		if (strpos($_POST['comments'],'\n') == TRUE) { $_POST['comments'] = str_replace('\n',"   ",$_POST['comments']); }
		if (strpos($_POST['comments'],'\r') == TRUE) { $_POST['comments'] = str_replace('\r',"   ",$_POST['comments']); }
		if (strpos($_POST['comments'],'<br>') == TRUE) { $_POST['comments'] = str_replace('<br>',"   ",$_POST['comments']); }
Edit:
But this does :
PHP:
		if (strpos($_POST['comments'],'
') == TRUE) { $_POST['comments'] = str_replace('
',"   ",$_POST['comments']); }

Is there not a better way to do this?
Edit:
I know these to be cariage returns:
PHP:
'\n'
'\r'
'<br>'

Are there others?
Edit:
Ok it only works locally now and not on the uploaded version.
 
Last edited:

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
The uploaded version works fine for me, although it seems to be a rather poor way to validate data.

Each datatype should be handled separately and you only check if someone filled in something.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
The form is only at the point where it checks whether the required fields are null or not. Once this is confirmed it will check each feilds entry for the required charatcters and lengths. It seem silly to make it check weather the e-mail address has an @ in it if the form isnt completed right. This means that every time the user gets sent back to the form to reatempt to fill in the fields the cpu would have to do this huge check again and again. So if you ask me it seems like a good way to just check on null fields and then when everything that is needed is entered then the major work can be done.

Correct me if i am wrong, it just seems logical to me.
 

Sohail

Active Member
Messages
3,055
Reaction score
0
Points
36
Maybe you need to upgrade your PHP. Have you tried that because sometimes the most advanced problems can have a simple answer.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
The problem only arises when an array that that contains line feeds is echoed to javascript function. If the array has no line feeds in it then its fine. I have tried trim and itrim also, no difference there.

@sohailamir52: My php version is on 2 already. I cant beleive that version wouldnt support the basics that I am using. Besides that it would throw out an error if there was something wrong with the php.
 
Top