简体   繁体   English

如何将动态生成的字段数据插入数据库Wordpress插件

[英]How to insert data of fields generated dynamically into database wordpress plugin

I am running a form which is posting data to database in such a way that every question field is having many options that are generated dynamically, the question query is working good but the option that are created dynamically(that query runs to 500) is not working. 我正在运行一种将数据发布到数据库的表单,使得每个问题字段都有许多动态生成的选项,问题查询运行良好,但动态创建的选项(查询运行到500)却没有工作。

front end: 前端:

<input name="text" placeholder="Question text" type="text" id="text"></br>
    <input type=\"text\" placeholder=\"text\" name=\"option_text[]\" class=\"fieldname\" />
    <input type=\"number\" placeholder=\"0\" name=\"option_score[]\" class=\"fieldtype\" />

jquery: which is functioning correctly jQuery:运作正常

function abc(){

var fName = new Array();

jQuery('.fieldname').each(function(index, value){
       fName.push(jQuery(this).val());
})

var fType = new Array();

jQuery('.fieldtype').each(function(index, value){
       fType.push(jQuery(this).val());
})

jQuery.ajax({
        type: 'POST',
        url: "<?php echo admin_url('admin-ajax.php'); ?>",
        data: { action: 'savedataques', text: document.getElementById('text').value, textopt: fName, score: fType},              
        success: function(data){
            alert('success');
alert('data');
            console.log(data);
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
}

The Php code: the data query is good but the dataop query runs int the error of blank page. Php代码:数据查询很好,但dataop查询运行为空白页错误。

    function savedataques(){
    global $wpdb;

            $data = ($wpdb->insert('wp_dbquestions', array(

                        'text'        => $_POST['text'],
                    )
            ));


 $lastid = $wpdb->insert_id;
            echo $lastid;

this is where the problem is: 这是问题所在:

$dataop = ($wpdb->insert('wp_questoptions', array(
                'question_id'        => $lastid
                'text'        => $_POST['textopt'],
                'score'        => $_POST['score'],
            )
    ));

        }

    exit;

die();
return true;
}
//
add_action('wp_ajax_savedataques', 'savedataques'); 
//add_action('wp_ajax_nopriv_savedataques', 'savedataques');

this is the solution: just need to apply a foreach on all the dynamically generated options 这是解决方案:只需在所有动态生成的选项上应用foreach

    $i=0;
    $optiontext = $_POST['textopt'];
    foreach($optiontext as $key => $val ){

        $score = $_POST['score'][$i];
        $textopt = $_POST['textopt'][$i];
        $i++;

then the query: 然后查询:

        $data = ($wpdb->insert('wp_questoptions', array(
                    'question_id' =>  $lastid,
                    'text'        => $textopt,
                    'score'        => $score,
                )
        ));

done 完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM