如何在PHP foreach中使用ajax?

时间:2016-05-12 09:22:01

标签: javascript php html arrays ajax

我有一个包含两个选择html标签和输入提交的表单。要填充选项标签的值并显示所选选项标签的等效值,我使用PHP,请参阅下面的代码段。

现在,我想使用JS来避免在用户点击按钮时重新加载浏览器。但我不知道怎么做。请帮帮我

这是link

摘录:

if(isset($_POST['mall_list'])){
    $mall_list= $_POST['mall_list'];
    $malls= $wpdb->get_results($wpdb->prepare("SELECT stores FROM tablename WHERE malls = '" . $mall_list. "' GROUP BY stores ORDER BY stores", OBJECT));

    echo '<div class="\record\">';
    foreach ($malls as $record){
        echo '<div>' . $record->stores . '</div>';
    }
    echo '</div>';

} elseif(isset($_POST['store_list'])){
    $store_list= $_POST['store_list'];
    $stores= $wpdb->get_results($wpdb->prepare("SELECT malls FROM tablename WHERE stores= '" . $store_list. "' GROUP BY malls ORDER BY malls", OBJECT));

    echo '<div class="\record\">';
    foreach ($stores as $record){
        echo '<div>' . $record->malls. '</div>';
    }
    echo '</div>';
}

3 个答案:

答案 0 :(得分:2)

如果你想通过ajax jquery发布数据。这段代码适合你。

$( "form" ).submit(function( event ) {
        event.preventDefault();
           $.ajax({
                type: "POST",
                url: "your post url",
                data: $('#yourformname').serialize(),
                success: function (data)
                {
                }
            });
});

答案 1 :(得分:2)

<强> HTML

<form name="ajaxform" id="ajaxform" action="ajax-form-submit.php" method="POST">
    First Name: <input type="text" name="fname" value =""/> <br/>
    Last Name: <input type="text" name="lname" value ="" /> <br/>
    Email : <input type="text" name="email" value=""/> <br/>
</form>

<强> JAVASCRIPT

//callback handler for form submit
$("#ajaxform").submit(function(e)
{
    var postData = $(this).serializeArray();
    var formURL = $(this).attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR) 
        {
            //data: return data from server
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            //if fails      
        }
    });
    e.preventDefault(); //STOP default action
    e.unbind(); //unbind. to stop multiple form submit.
});

$("#ajaxform").submit(); //Submit  the FORM

答案 2 :(得分:0)

的javascript

$("#form").submit(function(e){
   var data = $(this).serialize();
   var url = $(this).attr("action");
   $.post({
       url,
       data,
       function(res)
       {
           if(res.code == 0)
           {
               //success
               //code somthing when the server response
               alert(res.message);

           }
       }
   });
})

服务器

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    #is ajax request
    # code your business logic

    #response data 
    $response = [
       'code' => 0,
       'message' => 'success',
       'data' => []
    ];
    echo json_encode($response);exit;
} else {
    #is normal request
}