SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed in sending a form to javascript and then to PHP

    Hi friends

    I'm trying to create a new user function so users can enter data and become a member of my site but it won't work. I have finished coding all the javascript and PHP script validation and sending to server code but I don't know how to join them like this:

    'User submits form' --> 'javascript validates form' --> 'PHP validates and sends form values to database'


    All I need to know is how to connect the different functions. How would I do this?

    Thanks.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all write a JavaScript function that returns true if the JavaScript validation has passed, False otherwise.

    Then, in the HTML:
    PHP Code:
    <form action="yourPHPFile.php" method="post" onsubmit="return yourFunction();"
    If you return false from yourFunction, the form will not submit to your PHP validation, otherwise it will.

    You then treat it like every other form in your PHP validation. Just make sure you are double checking everything in PHP as you do in JavaScript, as JavaScript can be bypassed.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your form and javascript will look like this:
    Code html4strict:
    <html>
    <head>
    	<title>test</title>
    	<script type="text/javascript">
            funciton validate(frm){
                if(frm.yourname.value == ''){
                    alert('Enter your name.');
                    return false;
                }
                return true;
            }
    	</script>
    </head>
    <body>
        <form name="frm1" method="post" action="process.php" onsubmit="return validate(this);">
            Your name: <input name="yourname" type="text" id="yourname" />
            <br /><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
        </form>
    </body>
    </html>

    And your server script will look like this:
    PHP Code:
    $yourname $_POST['yourname'];
    if(!empty(
    $yourname)){
        echo 
    'You missed to enter your name';
    }
    else{
        
    // connect to database
        // select database
        // prepare a insert statement
        // execute the insert statement with mysql_query function.

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make a form that submits to the php script.
    No javascript yet.
    Test and refine until complete.

    Create a javascript function that will return true or false depending on whether or not the form's fields are valid.
    Use the submit event of the form element to call the function as the event handler. Returning false from the event handler function will prevent the form from submitting.

    edit-I'm too slow


Bookmarks

Posting Permissions

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