Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Form Validation code problem

    Hi

    can anyone tell me why this code is not working on my site.

    <script type="text/javascript">
    function validate_required(website, alerttxt)
    {
    with (website)
    {
    if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
    else
    {
    return true;
    }
    }
    }

    function validate_form(thisform)
    {
    with (thisform)
    {
    if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false)
    {website.focus();return false;}
    }
    }
    </script>


    and i have added this to the form
    <form id="myForm" action="data.php" method="post" onsubmit="return validate_form(this)">

    Yes the required field is "website"

    Basically the alert box pops up and says the error yet the forms still submits.

    I have no idea why. Please help.

  • #2
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i also have this code in my html

    <script type="text/javascript">
    $(document).ready(function() {
    $('#myForm').ajaxForm({
    target: '#showdata',
    success: function() {
    $('#showdata').fadeIn('slow');
    }
    });
    });
    </script>

    becuause my form refreshes without refreshing the page i think thats why i have got the problem.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just tried this locally on my system and it seems to be working fine for me. I added the div showdata to complete the example.

    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function validate_required(website, alerttxt) {
       with (website) {
          if (value==null||value=="") {
             alert(alerttxt);return false;
          } else {
             return true;
          }
       }
    }
    
    function validate_form(thisform) {
       with (thisform) {
          if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false) {
             website.focus();return false;
          }
       }
    }
    </script>
    </head>
    <body>
       <form id="myForm" action="test.php" method="post" onsubmit="return validate_form(this);">
       	website : <input type="text" name="website"/> <input type="submit" value="submit">
       	<div style="display:none;" id="showdata">Here it is</div>
       </form>
       
    </body>
    <script type="text/javascript">
    $(document).ready(function() {
       $('#myForm').ajaxForm({target: '#showdata',success: function() {
          $('#showdata').fadeIn('slow');
       }});
    });
    
    </html>

  • #4
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes that does work like that but for some reason it won't work on my php let me show you the whole document.

    HTML/PHP Document
    ========================================================
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Ajax JQuery Refresh Table </title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="includes/js/jquery.form.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    $('#myForm').ajaxForm({
    target: '#showdata',
    success: function() {
    $('#showdata').fadeIn('slow');
    }
    });
    });
    </script>

    <script type="text/javascript">
    function validate_required(website, alerttxt)
    {
    with (website)
    {
    if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
    else
    {
    return true;
    }
    }
    }

    function validate_form(thisform)
    {
    with (thisform)
    {
    if (validate_required(website,"Website Slogan/Description Must Be Filled Out!")==false)
    {website.focus();return false;}
    }
    }
    </script>

    </head>
    <body onload = "setTimeout('enable()', 5000)">


    <script type = "text/javascript">
    function enable() {
    var x = document.getElementById("reload");
    x.value = "Submit";
    x.disabled = false;
    }

    function disable() {
    var x = document.getElementById("reload");


    x.value = "Wait 30 secs";
    x.disabled = true;
    setTimeout('enable()', 30000);
    }


    </script>

    <form id="myForm" action="data.php" method="post" onsubmit="return validate_form(this)">
    <fieldset>
    <div>
    <label>slogan</label>
    <input type="input" name="ad" style="width:300px; margin:0 0 0 16px; color:#999;" id="cname" class="required"/>

    </div>
    <div>
    <label>Website </label>
    <input type="input" o name="website" style="width:300px; color:#999" class="required" />
    </div>
    <div>
    <input type ="submit" id = "reload" value = "Submit" onclick = "disable()" disabled>
    </div>
    </fieldset>
    </form>


    <div id="showdata">
    <?php

    $website= $_POST["website"];

    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("dbname");

    $result = mysql_query("SELECT * FROM ads ORDER BY uniqueid DESC");

    echo "<table border='0'>
    <tr>
    <th align='left' style='font-size: 25px; color: gray; text-decoration: underline;'>table</th>

    </tr>";

    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['ad'] . "</td>";

    echo "</tr>";
    echo "<tr>";
    echo "<td>" . $row['website'] . "</td>";

    echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>

    </div>


    </body>
    </html>

  • #5
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What browser are you using? IE8 is having an issue with the disable function. It seems to be ok in FireFox. Not sure why IE is having an issue. When you disable the button before the return it doesn't submit the form. If you comment out the line where you disable it, it submits the form fine in IE.

    Also make sure your action in your form tag is a real action otherwise you'll never get to you ajaxForm success function.

    strat

  • #6
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by stratocaster View Post
    What browser are you using? IE8 is having an issue with the disable function. It seems to be ok in FireFox. Not sure why IE is having an issue. When you disable the button before the return it doesn't submit the form. If you comment out the line where you disable it, it submits the form fine in IE.

    Also make sure your action in your form tag is a real action otherwise you'll never get to you ajaxForm success function.

    strat
    I am using firefox

    The form and everything works fine and even the validation works but after the popup box has appeared the form still submits.

    Is it not something to do with the form submitting without the page refreshing?

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    yes that does work like that but for some reason it won't work on my php let me show you the whole document.

    HTML/PHP Document
    That's not pure HTML. It contains many PHP snippets which can't be parsed at our end to produce the complete HTML. Take the html source from your browser's "view source" option and post it here.

    PS: Please use [CODE][/CODE] tags to wrap your code while posting here. Thanks!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •