1

I can see the values passed in the console but get nothing in the db and output is undefined errors. I d'ont think that the error is in my JS, I think the error is in how the PHP file grabs the data from .post

Here is my code:

Form:

<form id="data_form" action="" method="post">
    <input id="csActvSkuIp" name="csActvSkuIp" type="hidden" value="" />
    <input id="csActvSkuDtlsIp" name="csActvSkuDtlsIp" type="hidden" value=""  />
    <input id="csActvSkuCasePickIp" name="csActvSkuCasePickIp" type="hidden" value=""  />
    <input id="csActvSkuCasePickDtlsIp" name="csActvSkuCasePickDtlsIp" type="hidden" value=""  />
    <input id="csInputPicksPerMonthIp" name="csInputPicksPerMonthIp" type="hidden" value=""  />
    <input id="csInputPickRateIp" name="csInputPickRateIp" type="hidden" value=""  />
    <input id="csInputPickWrkr1Ip" name="csInputPickWrkr1Ip" type="hidden" value=""  />
    <input id="csInputPickWrkr2Ip" name="csInputPickWrkr2Ip" type="hidden" value=""  />
    <input id="csInputPickWrkr3Ip" name="csInputPickWrkr3Ip" type="hidden" value=""  />
    <input id="csInputPrcntPckingIp" name="csInputPrcntPckingIp" type="hidden" value=""  />
    <input id="csInputPickCostIp" name="csInputPickCostIp" type="hidden" value=""  />
    <input id="csCurrentPcpmIp" name="csCurrentPcpmIp" type="hidden" value=""  />
    <input id="csSubscriptionPerMonthIp" name="csSubscriptionPerMonthIp" type="hidden" value=""  />
    <input id="csPrcPerPickIp" name="csPrcPerPickIp" type="hidden" value=""  />
    <input id="csCartOnlySaveIp" name="csCartOnlySaveIp" type="hidden" value=""  />
    <input id="csSubPerMnLightsIp" name="csSubPerMnLightsIp" type="hidden" value=""  />
    <input id="csPrcPerPickLightsIp" name="csPrcPerPickLightsIp" type="hidden" value=""  />
    <input id="csCartOnlySaveLightsIp" name="csCartOnlySaveLightsIp" type="hidden" value=""  />

    <input type="submit" id="submit" name="submit" class="btn btn-info" />
</form>

jQuery:

<script type="text/javascript">
    $( document ).ready(function() {
        // process the form
        $('#data_form').submit(function(event) {
            alert('form submited');

            // get the form data
            var formData = {
                csActvSkuIp                 : $( "input[name=csActvSkuIp]" ).val(),
                csActvSkuDtlsIp             : $( "input[name=csActvSkuDtlsIp]" ).val(),
                csActvSkuCasePickIp         : $( "input[name=csActvSkuCasePickIp]" ).val(),
                csActvSkuCasePickDtlsIp     : $( "input[name=csActvSkuCasePickDtlsIp]" ).val(),
                csInputPicksPerMonthIp      : $( "input[name=csInputPicksPerMonthIp]" ).val(),
                csInputPickRateIp           : $( "input[name=csInputPickRateIp]" ).val(),
                csInputPickWrkr1Ip          : $( "input[name=csInputPickWrkr1Ip]" ).val(),
                csInputPickWrkr2Ip          : $( "input[name=csInputPickWrkr2Ip]" ).val(),
                csInputPickWrkr3Ip          : $( "input[name=csInputPickWrkr3Ip]" ).val(),
                csInputPrcntPckingIp        : $( "input[name=csInputPrcntPckingIp]" ).val(),
                csInputPickCostIp           : $( "input[name=csInputPickCostIp]" ).val(),
                csCurrentPcpmIp             : $( "input[name=csCurrentPcpmIp]" ).val(),
                csSubscriptionPerMonthIp    : $( "input[name=csSubscriptionPerMonthIp]" ).val(),
                csPrcPerPickIp              : $( "input[name=csPrcPerPickIp]" ).val(),
                csCartOnlySaveIp            : $( "input[name=csCartOnlySaveIp]" ).val(),
                csSubPerMnLightsIp          : $( "input[name=csSubPerMnLightsIp]" ).val(),
                csPrcPerPickLightsIp        : $( "input[name=csPrcPerPickLightsIp]" ).val(),
                csCartOnlySaveLightsIp      : $( "input[name=csCartOnlySaveLightsIp]" ).val(),
            };
            //alert(formData.activesku);

            console.log( formData );


            // process the form
            $.post('<?php echo $csamurl ; ?>', function(formData) {

                // place success code here
                $("#feedback").html(formData);
                alert(csActvSkuIp)

            })
                .fail(function(data) {
                    alert('this didnt work')
                });

            // stop the form from submitting the normal way and refreshing the page
            event.preventDefault();
        });
    });
</script>


<div id="feedback"></div>

PHP:

$ascasedtlsd        = $_POST["csActvSkuCasePickDtlsIp"];
$activeskud         = $_POST["csActvSkuIp"];
$askudttlsd         = $_POST["csActvSkuDtlsIp"];
$ascasepickd        = $_POST["csActvSkuCasePickIp"];
$pickspermonthd     = $_POST["csInputPicksPerMonthIp"];
$pickrated          = $_POST["csInputPickRateIp"];
$pickwrkroned       = $_POST["csInputPickWrkr1Ip"];
$pickwrkrtwod       = $_POST["csInputPickWrkr2Ip"];
$pickwrkrthrd       = $_POST["csInputPickWrkr3Ip"];
$prcntpickd         = $_POST["csInputPrcntPckingIp"];
$pickcostd          = $_POST["csInputPickCostIp"];
$currentpcpmd       = $_POST["csCurrentPcpmIp"];
$subpmad            = $_POST["csSubscriptionPerMonthIp"];
$pprmnthad          = $_POST["csPrcPerPickIp"];
$cartonlyd          = $_POST["csCartOnlySaveIp"];
$subpmbd            = $_POST["csSubPerMnLightsIp"];
$ppmnthbd           = $_POST["csPrcPerPickLightsIp"];
$cartlightd         = $_POST["csCartOnlySaveLightsIp"];


global $wpdb;

$table_name = $wpdb->prefix . 'codeshelf';

$wpdb->insert(
$table_name,
array(
'time' => current_time( 'mysql' ),
'activesku'         => $activeskud,
'askudttls'         => $askudttlsd,
'ascasepick'        => $ascasepickd,
'ascasedtls'        => $ascasedtlsd,
'pickspermonth'     => $pickspermonthd,
'pickrate'          => $pickrated,
'pickwrkrone'       => $pickwrkroned,
'pickwrkrtwo'       => $pickwrkrtwod,
'pickwrkrthr'       => $pickwrkrthrd,
'prcntpick'         => $prcntpickd,
'pickcost'          => $pickcostd,
'currentpcpm'       => $currentpcpmd,
'subpma'            => $subpmad,
'pprmntha'          => $pprmnthad,
'cartonly'          => $cartonlyd,
'subpmb'            => $subpmbd,
'ppmnthb'           => $ppmnthbd,
'cartlight'         => $cartlightd,
)
);

This is a WordPress plugin I am working on, but I have tested the post using

if (isset($_POST['submit'])) {

The values of the form are being set with a click function and this works also, the only thing that I am not having any luck with is this.

1
  • You need to enclose form value with single quote input[name='value'] or refer to my answer below. Commented Oct 23, 2014 at 9:13

2 Answers 2

1

you can pass the form itselt. try it.

$.post( "test.php", $( "#testform" ).serialize() );
Sign up to request clarification or add additional context in comments.

1 Comment

You were right it was much easier and the lines of code much smaller. In the end this helped a ton! My real issue was calling $wpdb on the next page, figured it out by doing var_dump() and when it returned null I knew there was an issue. Right now I am using include wp-load.php to initialize the wpdb object on the page I am passing the data to, bad form though so I am going to dig into the hooks for wp more. Thanks a bunch for all of your help, both of you!
0

But your var formData does not include the submit:

submit: $( "input[name=submit]" ).val(),

But as said @jay using .serialize() would be much more easier.

1 Comment

Thanks for the info, I would mark you guys both but new here.

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.