+ Reply to Thread
Results 1 to 7 of 7

Thread: I am having trouble with a form, help please!

  1. #1
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    I am having trouble with a form, help please!

    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 Code:
    <?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 by DefecTalisman; 11-12-2007 at 02:34 PM.

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  2. #2
    mitamata's Avatar
    mitamata is offline x10Hosting Member mitamata is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Canada
    Posts
    81

    Re: I am having trouble with a form, help please!

    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.

  3. #3
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: I am having trouble with a form, help please!

    even when I use:

    PHP Code:
            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 Code:
            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 Code:
            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 Code:
    '\n'
    '\r'
    '<br>' 
    Are there others?
    Edit:
    Ok it only works locally now and not on the uploaded version.
    Last edited by DefecTalisman; 11-12-2007 at 03:24 PM. Reason: Automerged Doublepost

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  4. #4
    Slothie's Avatar
    Slothie is offline Lord Of The Keys Slothie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Singapore
    Posts
    1,432

    Re: I am having trouble with a form, help please!

    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.

    Easiest 70 points you'll make on x10

    Feel free to add my reputation by clicking on the if you found my post helpful to you :P


    If I am not responding to your PMs, that means I am ignoring you. Take a hint.



    09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0


  5. #5
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: I am having trouble with a form, help please!

    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.

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



  6. #6
    Sohail's Avatar
    Sohail is offline x10 Spammer Sohail is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    London, UK
    Posts
    3,052

    Re: I am having trouble with a form, help please!

    Maybe you need to upgrade your PHP. Have you tried that because sometimes the most advanced problems can have a simple answer.

  7. #7
    Join Date
    Aug 2007
    Location
    Gangstas Paradise
    Posts
    4,143

    Re: I am having trouble with a form, help please!

    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.

    http://dev.x10hosting.com (this has nothing to do with x10hosting)

    ->All us helpful people here at x10hosting would like to reach our next user groups, "Community Paragon". Please click the +rep icon on the left hand side of a post if that post was helpfull.



+ Reply to Thread

Similar Threads

  1. REQ - PHP Form Submission Form
    By dwedno in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 11-29-2007, 06:38 PM
  2. Design a free form
    By gateway in forum Off Topic
    Replies: 1
    Last Post: 10-19-2007, 05:49 PM
  3. My php Form tutorial
    By WinGate in forum Tutorials
    Replies: 0
    Last Post: 08-20-2007, 08:14 PM
  4. Frontpage Form Problem
    By geek2009 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 07-03-2007, 10:18 AM
  5. A simple Visual Basic Login Form
    By Zenax in forum Tutorials
    Replies: 0
    Last Post: 03-13-2007, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers