Wordpress表单,将选择值提交为字符串

时间:2017-08-03 12:29:47

标签: ajax wordpress

我有一个有三个选择的表单我正在调用trhough ajax。 我将选择值作为字符串传递,然后使用php explode函数检索两个类别名称以填充$ cate = $ _POST [' cat_prod']和id来处理和提取ajax产品。 我收到404页面。 这是代码



                        <?php 
                        global $wpdb;
                        $aCategoria = $wpdb->get_results( "SELECT * FROM categorie" );
                        ?>
                        <select name="cat_prod[]" class="categorie">
                            <option value="null">--SELEZIONA CATEGORIA--</option>
                            <?php
                            foreach ($aCategoria as $categoria) {
                                ?>
                                <option value="<?php $opt = array($categoria->id_cat, $categoria->categoria); echo $opt[1]."/".$opt[0]; ?>"><?php echo $categoria->categoria; ?></option>
                                <?php 
                            }
                            ?>
                        </select>
&#13;
&#13;
&#13;

进行ajax调用我使用以下脚本

&#13;
&#13;
	var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>';
        
        // prodotti
	jQuery(function($) {
		$('body').on('change', '.categorie', function() {                    
			var categorie = $(this).val();console.log(categorie);
			if(categorie != "") {                     
				var data = {
					action: 'get_prodotti_by_ajax',
					categorie: categorie,
                                        security: '<?php echo wp_create_nonce('get_prodotti'); ?>'
			}

				$.post(ajaxurl, data, function(response) {
					$('.load-prodotti').html(response);
			});
			}
		});
	});
&#13;
&#13;
&#13;

并将其他选择以下函数放入functions.php(wordpress)

&#13;
&#13;
add_action('wp_ajax_get_prodotti_by_ajax', 'get_prodotti_by_ajax_callback');
add_action('wp_ajax_nopriv_get_prodotti_by_ajax', 'get_prodotti_by_ajax_callback');
function get_prodotti_by_ajax_callback() {
        check_ajax_referer('get_prodotti', 'security');
	$cate = $_POST['categorie'];
        
        // inizio
        
        $idc = explode("/", $cate);
        $idcate = $idc[1];
        
        // fine
        
	global $wpdb;
	$aProdotti = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM prodotti WHERE id_categoria = '$idcate' " ) );
	if ( $aProdotti ) {
		?>
                <select class="prodotti" name="prodotti[]" style="width: 100%">
                    <option value="null">seleziona prodotto</option>
			<?php
			foreach ($aProdotti as $prod) {
				?>
				<option value="<?php $opt=array($prod->id, $prod->prodotto); echo $opt[1]."/".$opt[0]; ?>"><?php echo $prod->prodotto; ?></option>
				<?php
			}
			?>
		</select>
		<?php
	}
	wp_die();
}
&#13;
&#13;
&#13;

我必须说所有ajax工作正常,但是当我提交表单时,我会收到404错误页面。 我做错了什么?

这是总表格

&#13;
&#13;
<form method='post' action="<?php the_permalink(); ?>">
                <table id="tab_prev">
                    <tbody>
                  <tr>
                    <th style="width: 26px;"><input class='check_all' type='checkbox' onclick="select_all()"/></th>
                    <th>No</th>
                    <th>Categoria</th>
                    <th style="width: 120px">Prodotto</th>
                    <th style="width: 120px">Misura-Kg/ml</th>
                    <th>Quantità</th>
                    <th>Lungh. mm</th>
                    <th>Tot/mt</th>
                    <th>Tot/kg</th>
                    <th>kg/mt</th>
                    <th>Val €</th>
                    <th>Tot €uro</th>
                  </tr>
                  <tr>
                    <td><input type='checkbox' class='case'/></td>
                    <td>1.</td>
                    <td>
                        <?php 
                        global $wpdb;
//                        $aCategoria = $wpdb->get_results( "SELECT id_cat, categoria FROM categorie" );
                        $aCategoria = $wpdb->get_results( "SELECT * FROM categorie" );
                        ?>
                        <select name="cat_prod[]" class="categorie">
                            <option value="null">--SELEZIONA CATEGORIA--</option>
                            <?php
                            foreach ($aCategoria as $categoria) {
                                ?>
                                <option value="<?php $opt = array($categoria->id_cat, $categoria->categoria); echo $opt[1]."/".$opt[0]; ?>"><?php echo $categoria->categoria; ?></option>
                                <?php 
                            }
                            ?>
                        </select>
                        

                    </td>
                    <td class="load-prodotti"><select disabled="disabled" style="width: 100%;"><option>--seleziona</option></select></td>
                    <td class="load-misure"><select disabled="disabled"><option>--seleziona</option></select></td>
                    <input type="hidden" name="pesi[]" id="kg" class="load-peso" >
                    <td><input type="number" id="q" name="quantita[]" min="1" placeholder="quantit&agrave;"></td>
                    <td><input type="number" id="l" name="lunghezza[]" min="1"step="1" pattern="[0-9]" placeholder="lunghezza"></td>
                    <td><input type='number' class='total_mt' name='total_mt[]' placeholder="0,00" data-ac="(#q*#l/1000)" readonly/></td>  
                    <td><input type="number" class='total_kg' name="total_kg[]" placeholder="0,00" data-ac="(#kg*#q*#l/1000)" readonly/></td> 
                    <td><select name="calcolo[]" id="calcolo" onchange="condition()" style="width: 80px">
                            <option value="null">--</option>
                            <option value="0" >€/Kg</option>
                            <option value="1">€/Mt</option>
                        </select>
                    </td>
                    <td><input type="number" name="quotazione[]" step="0.001" id="e" min=0 placeholder="0,00"></td>
                    <td id="valore_tot"><input type="number" placeholder="0,00" readonly style="background-color: rgba(0,0,0,0.1);"></td>
                  </tr>
                  </tbody>
                </table>
                <p>
                <button type="button" class='delete' title="Rimuovi"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
                <button type="button" class='addmore' title="Aggiungi"><i class="fa fa-plus" aria-hidden="true"></i></button>
                </p>
                <textarea placeholder="Messaggio" rows="5" cols="150" name="messaggio"></textarea>
                <p>
                    <label style="width:100px;">Totale Kg </label><input type="number" id="tot_kg_gross" name="tot_kg_gross" readonly>
                </p>
                <p>
                    <label style="width:100px;">Totale € </label><input type="number" id="tot_eur_gross" name="tot_eur_gross" readonly>
                </p>
                <p>
                    <input type="reset" value="Annulla" style="width:80px;"><input type='submit' name='invia' value='Invia' class='but'  style="width:80px;margin-left: 10px;"/>
                </p>
            </form>  
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

just try this.

    function get_prodotti_by_ajax() {   
    check_ajax_referer('get_prodotti', 'security');
    $cate = $_POST['categorie'];

        // inizio

        $idc = explode("/", $cate);
        $idcate = $idc[1];

        // fine

    global $wpdb;
    $aProdotti = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM prodotti WHERE id_categoria = '$idcate' " ) );
    if ( $aProdotti ) {
        ?>
                <select class="prodotti" name="prodotti[]" style="width: 100%">
                    <option value="null">seleziona prodotto</option>
            <?php
            foreach ($aProdotti as $prod) {
                ?>
                <option value="<?php $opt=array($prod->id, $prod->prodotto); echo $opt[1]."/".$opt[0]; ?>"><?php echo $prod->prodotto; ?></option>
                <?php
            }
            ?>
        </select>
        <?php
    }
    wp_die();
}
add_action('wp_ajax_get_prodotti_by_ajax', 'get_prodotti_by_ajax');
add_action('wp_ajax_nopriv_get_prodotti_by_ajax', 'get_prodotti_by_ajax');

here is the var ajaxurl

	var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>';
        
        // prodotti
	jQuery(function($) {
		$('body').on('change', '.categorie', function() {                    
			var categorie = $(this).val();console.log(categorie);
			if(categorie != "") {                     
				var data = {
					action: 'get_prodotti_by_ajax',
					categorie: categorie,
                                        security: '<?php echo wp_create_nonce('get_prodotti'); ?>'
			}

				$.post(ajaxurl, data, function(response) {
					$('.load-prodotti').html(response);
			});
			}
		});
	});

here attached screenshot enter image description here