0

i am using a form validation plugin and in my js file i have this for input and checkbox value pass to json

$('form').submit(function(event) {

    $('.form-group').removeClass('has-error'); // remove the error class
    $('.help-block').remove(); // remove the error text
    var chkbox = new Array();
    $('input:checked').each(function() {
        chkbox.push(parseint($(this).val()));
    });

    var formData = {
        'company_name': $('input[name=company_name]').val(),
    }

    $.ajax({
        type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url: 'process.php',
        data: formData,chbox,
        dataType: 'json',
        encode: true
    }).done(function(data) {
        console.log(data); 
        if ( ! data.success) {
            if (data.errors.company_name) {
                $('#company_name-group').addClass('has-error');
                $('#company_name-group').append('<div class="help-block">' + data.errors.company_name + '</div>');

Then in php

$errors         = array(); 
$data           = array(); 

                if (empty($_POST['company_name']))
                    $errors['company_name'] = 'Company Name is required.';
                if ( ! empty($errors)) {
                    // if there are items in our errors array, return those errors
                    $data['success'] = false;
                    $data['errors']  = $errors;
                } else {
                    $company_name=mysqli_real_escape_string($con,$_POST['company_name']);
                    $chkbox = $_POST['chkbox'];
                    $chkNew = "";  
                foreach($chkbox as $chkNew1)  
                {
                    $chkNew .= $chkNew1 . ",";  
                }  

                $ins_query="insert into xxx(`company_name`,`Job`)values('company_name','$chkNew');"
                retval = mysqli_query( $con ,  $ins_query);
                $data['success'] = true;
                $data['message'] = 'Success!';
            }
            echo json_encode($data);

This work perfectly fine for insert values of form on db values but it stops in my process.php file and doesnt show the message on page .When i remove chkbox from js file form is validated and shows message success in succesfull insertion to db .

1 Answer 1

1

The last character in $chkNew is ,.

You should remove it.

Try to add this after foreach:

$chkNew= trim($chkNew, ',');
Sign up to request clarification or add additional context in comments.

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.