Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:08:33+00:00 2026-06-10T15:08:33+00:00

Using various tutorials namely here and here I’ve managed to put together the following

  • 0

Using various tutorials namely here and here I’ve managed to put together the following PHP script which performs server side validation on the form being submitted. (I already have script which is dealing with the ‘client side’ validation.

<?php
//email signup ajax call
if($_GET['action'] == 'signup'){

    //sanitize data
    $email = mysql_real_escape_string($_POST['signup-email']);

    //validate email address - check if input was empty
    if(empty($email)){
        $status = "error";
        $message = "You did not enter an email address!";
    }
    else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
            $status = "error";
            $message = "You have entered an invalid email address!";
    }
    else {

            $insertSignup = mysql_query("INSERT INTO signups (signup_email_address) VALUES ('$email')");
            if($insertSignup){ //if insert is successful
                $status = "success";
                $message = "You have been signed up!";  
            }
            else { //if insert fails
                $status = "error";
                $message = "Ooops, Theres been a technical error!"; 
            }

    }

    //return json response
    $data = array(
        'status' => $status,
        'message' => $message
    );

    echo json_encode($data);
    exit;
}
?>

What I’m now trying to do is to add another field, in this case ‘name’ which I’d like to also validate.

The problem I’m having is that I’m not sure how to add another field into the above code. Again, I’ve been trying to find an example which I could use to study from, but I haven’t found any that I can use.

I just wondered whether someone could possibly look at this please, and perhaps point me in the right direction.

Many thanks and kind regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:08:35+00:00Added an answer on June 10, 2026 at 3:08 pm

    PHP has a Filter extension to validate and sanitize input.

    The function you are looking for is

    • filter_var_array — Gets multiple variables and optionally filters them

    There is also filter_input_array but since there is no easy way to unit-test that properly, it is easier to use the above one instead and pass it the superglobals as needed.

    Example:

    $userInput = array(
        'signup-email' => 'foo at example.com',
        'name' => 'ArthurDent42'
    );
    
    $validatedInput = filter_var_array(
        $userInput, 
        array(
            'signup-email' => FILTER_VALIDATE_EMAIL,
            'name' => array(
                'filter' => FILTER_VALIDATE_REGEXP,
                'options' => array(
                    'regexp' => "/^[a-z ]{5,10}$/i"
                )
            )
        )
    );
    
    var_dump($validatedInput);
    

    Output (demo):

    array(2) { 
        ["signup-email"]=> bool(false) 
        ["name"]=> bool(false)
    }
    

    Once you have the input validated and sanitized put some guard clauses for each of the values in the array and return early when they are false:

    if (!$validatedInput['signup-email']) {
        return json_encode(array(
            'status' => 'error',
            'message' => 'The eMail was invalid'
        ));
    }
    
    if (!$validatedInput['name']) {
        return json_encode(array(
            'status' => 'error',
            'message' => 'Name must be 5 to 10 letters from A to Z only'
        ));
    }
    
    // everything's validated at this point. Insert stuff to database now.
    

    Note that you want to use either PDO or mysqli instead of ext/mysql.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been dabbling abit with a login script (PHP and MySQL), using various online
I am currently using various back-end services and I want to use PHP to
Can someone suggest some good tutorials for getting started with JAX-WS? Using various tools
Hey there guys and gals. I'm very new to php and am following various
through the help of various tutorials, I've managed to write a custom dialog that
I'm using various functions from the earthdistance module in PostgreSQL, one of those being
My application scrapes information from various sites using Mechanize. Naturally, each site requires custom
I am using PDF documents for various purposes using iText library. Its like one
I'm trying to run various commands using psexec.exe from Windows Sysinternals . What I
I've written an application using wxPython and various other small modules (xlrd, xlwrt, pyserial,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.