How do I send a form to javascript and then to PHP

Discussion in 'PHP' started by Imozeb, Feb 11, 2010.

  1. #1
    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.
     
    Imozeb, Feb 11, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You don't want to do this. If a user disables javascript, the validation is bypassed.
     
    jestep, Feb 11, 2010 IP
  3. nadiralishah_webexpert

    nadiralishah_webexpert Guest

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    on forum submit event, you can call the function of javascript, name that function validations, and put basic validations there like user should enter his name, should not leave empty the email box, etc.. and if validation goes well, then form will be submitted to php page, u will set the php page on form action event. and php page will receive form data, php coding will connect to database and with insert sql query will insert the data into database table.
     
    nadiralishah_webexpert, Feb 11, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Okay. But can I make the PHP script be on the same page so when PHP validates it, the user doesn't lose all her imput if it returns false?
     
    Imozeb, Feb 11, 2010 IP
  5. jNorth

    jNorth Peon

    Messages:
    202
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you have to handle population the fields in PHP so the user doesn't lose their input. this is the standard way.
     
    jNorth, Feb 11, 2010 IP
  6. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes you can do it . make the form action to the same script and in the input field's value attribute , instead of

    
    
    <input type="text" name="foo" value="">
    
    
    Code (markup):
    change it to

    
    
    <input type="text" name="foo" value="<?php echo $_POST['foo']; ?>">
    
    
    Code (markup):
    Hope this helps!
     
    HivelocityDD, Feb 12, 2010 IP
  7. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #7
    
    
    <input type="text" name="foo" value="<?php echo $_POST['foo']; ?>">
    
    
    Code (markup):
    Just wondering, is it safe to echo user POST data straight back out like that, with no verification. I can't THINK of a way to exploit that for some nefarious purpose, but that doesn't mean someone else can't! Worth validating in PHP too in some way.
     
    markowe, Feb 12, 2010 IP
  8. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can add the onSubmit event to the forum to run the validation function when the form in submitted. Just add onSubmit where you declare the start form tag and put the javascript function in it. If you want to validate each field individually then you can place a onChange event on input text fields to validate when a user changes the text.
     
    Brandon.Add.On, Feb 12, 2010 IP
  9. Adfuel

    Adfuel Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    >'User submits form' --> 'javascript validates form' --> 'PHP validates and sends form values to database'

    validate with php first.

    if($_POST['formvar'] != $towhatyouwant){
    //go to error page
    }

    then setup the javascript to just stop the form from posting if
    you dont like input data.

    Just validating with javascript is not safe because users can bypass it.
     
    Adfuel, Feb 12, 2010 IP
  10. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #10
    Yes, you didn't say what the validation was for, but obviously you do the same (and other) validation checks with PHP afterwards too - the JS validation is just to save the user having to refresh your page, saves time on your server etc. but obviously if someone types in some bit of MySQL code into your email field you obviously want PHP to catch that serverside!
     
    markowe, Feb 13, 2010 IP
  11. nadiralishah_webexpert

    nadiralishah_webexpert Guest

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    yes, you can work on single php file instead of two files one html and other php. form will submit on the same page and you can keep the values like this;

    <input name='fname' type='text' value='<?php echo $_POST['fname']?>'>
    Code (markup):
     
    nadiralishah_webexpert, Feb 13, 2010 IP