使用jquery $ .post提交隐藏的输入值空db列和注意:未定义的索引

时间:2014-10-23 07:48:05

标签: jquery ajax post

我可以看到控制台中传递的值,但db中没有任何内容,输出是未定义的错误。我认为错误在我的JS中,我认为错误在于PHP文件如何从.post中获取数据

这是我的代码:

形式:

<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,
)
);

这是我正在处理的WordPress插件,但我已经使用

测试了帖子
if (isset($_POST['submit'])) {

表单的值是通过点击功能设置的,这也是有效的,我唯一没有运气的是这个。

2 个答案:

答案 0 :(得分:1)

你可以传递形式itselt。 试试吧。

$.post( "test.php", $( "#testform" ).serialize() );

答案 1 :(得分:0)

但您的var formData不包含提交内容:

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

但正如所说@jay使用.serialize()会更容易。