0

i am using following code to add values

$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];
include_once ("db.php");

$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";

now i want that if "debamt" is filled "debdes" should not be empty and like this if "cramt" is filled then "crdes" should not be empty while pressing submit button so if some one press submit button it displays a message that say fill "debdes" first.

4 Answers 4

3

What you're looking for is some basic form validation:

$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];
include_once ("db.php");

$errors = array();

// Check for error conditions
if(!empty($debamt) && empty($debdes)) {
    $errors[] = 'Fill also debdes';
}

if(!empty($cramt) && empty($crdes)) {
    $errors[] = 'Fill also crdes';
}

// If no errors were found, execute the query
if(count($errors) == 0) {
    $ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES     ('$debdes','$debamt','$crdes','$cramt','$date');";
} else {
    // If there are errors, display them:
    echo implode('<br />', $errors);
}
Sign up to request clarification or add additional context in comments.

Comments

2

On the previous form itself you can have the "submit" button trigger a javascript function as follows:-

function validate(){
    var debamt=document.*form_name*.debamt.value;
    var debdes=document.*form_name*.debdes.value;
    var error_message;
    if(debamt!=null && debdes==null){      
        error_message = 'Please fill debdes value first!!!';
    }
    var cramt=document.*form_name*.cramt.value;
    var crdes=document.*form_name*.crdes.value;
    if(cramt!=null && crdes==null){      
        error_message = '\nPlease fill crdes value first!!!';
    }
    alert(error_message);
}

1 Comment

Yes, but only in addition to server-side checking. Never rely on the client to do anything important.
1
 if( ! empty($_POST['debdes']) && empty($_POST['debdes']))
     die('fill "debdes" first');

the same goes for cramt and crdes.

Comments

1
$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];

if ( !empty($debamt) && empty($debdes) ) {
    show_error('debdes');
}

if ( !empty($cramt) && empty($crdes) ) {
    show_error('drdes');
}

function show_error($string) {
    echo "Value '$string' must be filled.";
    exit;
}

include_once ("db.php");

$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date)
           VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.