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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    137
    Thanks
    1
    Thanked 0 Times in 0 Posts

    issues with using form in php

    i have 3 pages page1.php page2.php and page3.php

    in page1.php user fills a registration form in page2.php the user can review what they have entered and make any changes and

    page3.php displays a message that the registration has been completed.

    i am having issues with javascript validation in page2.php

    in page1.php the javascript validation file is file1.js and in page2.php it is file2.js in page1.php the javascript code i

    have written works fine and does all the validation as the form elements in page1.php are written as
    <input type="text" name=""> <input type="radio" name=""> ...

    in page2.php i am displaying these form elements using php so that user can preview what they have entered and make changes

    instead of going back to page1.php the code to display the form elements in page2.php is

    <?php
    echo "<input type ='hidden' name='username' value='".stripslashes($username)."'>";
    echo "<input type =\"text\" name=\"username\" value=\"$username\">";
    ?>

    in page2.php for checkboxes the code is <input type="checkbox" name="" value=""> which is normal html unlike the textfields

    above using php

    what i have noticed in page2.php is the checkboxes are being validated however the text fields in page2.php do not validate

    properly and does not work as it works in page1.php

    what i am thinking is because the textfields are being generated using the above php code the javascript is not reading and

    validating however i am not sure what the exact reason is.

    sample code of javascript for file1.js is

    var username = document.registrationform.username
    if ((username.value==null) || (username.value=="") || (username.length=="") )
    {
    alert("Please Enter a User Name")
    username.value=""
    username.focus()
    return false
    }

    sample code of javascript for file2.js is

    var fname=document.registrationform.fname
    if ( (fname.value==null) || (fname.value=="") || (fname.length=="") )
    {
    alert("First Name is required")
    return false
    }
    also sometime if i use fname.value in page2.php i get an error that value is not an object or is null so i have modified the

    code in file2.js to some extent


    can some one tell me how to go about validating textfields and other form elements in page2.php using php code to be

    validated like the checkboxes in page2.php

    any help will be greatly appreciated.


    also in page2.php i have an insert statement which writes the information that a user has entered in the form into mysql

    database. i have read about database injection recently. so i have used $username = addslashes($_POST["username"]);
    due to this the value in the database is recorded as example first\name if a user typed first'name in the form should i use
    mysql_real_escape_string() method to take extra precaution or is addslashes() method alright. if i adopt

    mysql_real_escape_string() do i just read the values simply as $username = mysql_real_escape_string($_POST["username"]);
    or do i need to write extra code because of using mysql_real_escape_string() method

    thanks.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    username.length=="", fname.length=="" etc should be
    object.value.length

    BTW, why do you need 3 pages? Submit the form to the same page so that you can easily display the server side validation errors. Just show/hide the html portion depending upon success/failure.
    Last edited by abduraooft; 02-26-2008 at 07:22 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Try using Firefox with the web developer extension. The javascript debugger feature will point out these errors for you. I agree with abduraooft, 3 pages is overkill. You only need 1 page. The form should submit via POST and display any validation errors above the form for eg. If the record insert is successful, header() redirect to the same page with GET vars such as ?insert=ok. If insert isset, don't output the form and just display the success message...
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.


  •  

    Posting Permissions

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