我用

if(isset($_POST['category_drop'])){
    var_dump($_POST );
    echo $_POST['category_drop'];
}  {...} to check for the post condition in PHP.


请给我一个解决方案。

$('.category_filter .dropdown-menu li a ').on('click', function(e) {

    e.preventDefault();
    var category = $(this).text();

    $.ajax({
        type: "POST",
        url: 'page_author.php',
        data: {
            category_drop: category
        },
        success: function(data) {
            // do something;
            alert(category);
        }
    });
});




我在警告框中收到低于该值的值,但我的页面未加载相应的值,


  array(1){[“” category_drop“] => string(12)” Accomodation“}


住宿

最佳答案

下面的注释代码显示了如何实现目标:


    e.preventDefault();
    var category = $(this).text();

    $.ajax({
        type: "POST",
        url: 'page_author.php',
        data: {
            category_drop: category
        },
        success: function(data) {
            // PERHAPS IT'S NOT category BUT data THAT YOU SHOULD BE INTERESTED IN.
            // AND; YOU BETTER console.log()
            // TO AVOID THOSE ANNOYING JAVASCRIPT ALERTS...
            // ALTHOUGH, YOU MAY STILL ALERT THE category IF YOU WISHED...
            console.log(data);
            //alert(category);
        }
    });


在PHP方面:

<?php

    if(isset($_POST['category_drop'])){
        // ASSIGN THE $_POST['category_drop'] TO A VARIABLE $categoryDrop
        // TO BE USED IN YOUR MySQL DATABASE QUERIES...
        $categoryDrop = $_POST['category_drop'];

        // YOU CAN NOW USE $categoryDrop IN YOUR MySQL QUERIES.
        // AFTERWARDS, IF YOU WANT TO ONLY SEND BACK THE $categoryDrop
        // YOU CAN SIMPLY DO:
        //EITHER:
        die($categoryDrop);

    }

关于javascript - 当我尝试使用ajax post将值从JavaScript发送到PHP时,URL未加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39544605/

10-11 01:37