www.webdeveloper.com
Results 1 to 8 of 8

Thread: PHP/Javascript

Hybrid View

  1. #1
    Join Date
    Aug 2007
    Posts
    20

    PHP/Javascript

    Hello,

    I have a rough idea of what I am trying to produce - Contact Processing Script.

    What I am stuck on is how do I make the PHP process the script if all the javascript values are correct else do nothing due to the values not being correct. What do I need to set within the isset?



    PHP Code:
    if(isset($_POST["submit"])){



  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    PHP will have no idea what the JavaScript did, unless you somehow include the JavaScript results in the post/get data that is sent to the PHP script. (And note that JavaScript can be easily bypassed, either inadvertently by users with it disabled or maliciously by robot scripts or such, so you really need to do any important validation in the PHP script, too.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2007
    Posts
    20
    Quote Originally Posted by NogDog View Post
    PHP will have no idea what the JavaScript did, unless you somehow include the JavaScript results in the post/get data that is sent to the PHP script. (And note that JavaScript can be easily bypassed, either inadvertently by users with it disabled or maliciously by robot scripts or such, so you really need to do any important validation in the PHP script, too.)
    Yep thats what is getting me confused. I want the functional checking like JS in PHP but I no it cannot be done and I cannot find a good example of a PHP contact form. Any Ideas?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    A fairly simple, all-purpose approach is something like:
    PHP Code:
    $errors = array();
    if(empty(
    $_POST['name'])) {
       
    $errors[] = "You must enter a Name.";
    }
    if(empty(
    $_POST['email']) or filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) {
       
    $errors[] = "Please enter a valid email address.";
    }
    if(
    count($errors)) { // we have at least one error
       
    echo "<p class='error'>" implode("<br />\n"$errors) . "</p>\n";
    }
    else {
       
    // go ahead and process the form data here

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2007
    Posts
    20
    Thanks for that:

    Just a couple of questions

    1. Does The Filter function use regex?
    2. Could I use Trim in this statement if so were?
    3. Is captcha a good idea?

  6. #6
    Join Date
    Aug 2007
    Posts
    20
    I have been thinking and have a couple more:


    Just a couple of questions

    1. Does The Filter function use regex?
    2. Could I use Trim in this statement if so were?
    3. Is captcha a good idea?
    4. How would I impliment a required section?


    Instead of having a if statement for every field in the form could I have it like this? My concept in my head is fine but I am having an issue to explain it.

    PHP Code:

    $values 
    = new array('[B]gets data from the $_POST[/B]'); // I am unsure how I would do this?  I have used php for years but this has been one of my biggest confusions 


    if(empty($values)) {
       
    $errors[] = "Incorrect "'.$values.'" ";


  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Does The Filter function use regex?
    I don't know: probably, but it's a built-in function. It just makes sure it's reasonably valid syntax, but does not rigidly enforce the standards.

    Could I use Trim in this statement if so were?
    Sure. I often apply trim() to the $_POST array separately via array_walk_recursive() before doing anything else with it.

    Is captcha a good idea?
    Only if you think you really need it, as users find it annoying.

    How would I impliment a required section?
    Not sure what you mean, but I think that's what the empty() check would be fore. If you want to enforce minimum or maximum numbers of characters, you can use strlen() to check the length, or substr() to just truncate any extra characters if that's desired.

    Instead of having a if statement for every field in the form could I have it like this? My concept in my head is fine but I am having an issue to explain it.

    PHP Code:
    $values = new array('[B]gets data from the $_POST[/B]'); // I am unsure how I would do this?  I have used php for years but this has been one of my biggest confusions 

    if(empty($values)) {
       
    $errors[] = "Incorrect "'.$values.'" ";

    [/quote]
    You could set up some sort of array defining field names, types, minimum/maximum lengths, and so forth, then loop through that array to run a generic validation function on each.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Aug 2007
    Posts
    20
    My Current Code Has:

    PHP Code:

        $required_fields 
    = array('name''email','message'); // added 'checkbox' for required.
        
        
    foreach($_POST as $key => $value) {
            
            
    $_SESSION[$key] = trim($value);
            
            if(
    in_array($key$required_fields) && empty($value)) $errors[$key] = ucwords($key) .' is required!';
            
        } 
    How could I impliment this into your structure? Is it required?

    How would I do the trim() to the $_POST array separately via array_walk_recursive() before doing anything else with it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles