简体   繁体   English

使用jQuery Ajax和PHP进行单Div刷新

[英]Single Div refresh with jquery Ajax and PHP

Okay So I have a div on my page that has some code for display option groups in a select input. 好的,所以我的页面上有一个div,其中包含一些用于选择输入中显示选项组的代码。 And then on the other side displaying the options in that group after the selection is made. 做出选择后,在另一侧显示该组中的选项。 My html/php code for this is below: 我的html / php代码如下:

<div class="row">
    <div class="col-lg-6">
        <label class="control-label" for="productOptions">Select your
        product options</label> <select class="form-control" id=
        "productOptions">
            <option>
                Select an Option Group
            </option><?php foreach($DefaultOptions as $option): ?>

            <option value="<?php echo $option['GroupID']; ?>">
                <?php echo $option['GroupName']; ?>
            </option><?php endforeach; ?>
        </select>
    </div>

    <div class="col-lg-6" id="groupOptions">
        <label class="control-label">Group Options</label>
        <?php if($GroupOptions): ?>
        <?php foreach ($GroupOptions as $optionValue): ?>
        <?php echo $optionValue['optionName']; ?> <?php endforeach; ?>
        <?php endif; ?>
    </div>
</div>

By default on the original page load, $GroupOptions does not exist in the form, because it is set after the user selects the Group they wish to choose from. 默认情况下,在原始页面加载中, $GroupOptions不存在于表单中,因为它是在用户选择希望从中选择的组之后设置的。 I call the php script by using ajax to avoid page reload 我通过使用Ajax来调用php脚本以avoid page reload

$("#productOptions").change(function(){

    var GroupID = $(this).val();
    var dataString = 'GroupID=' + GroupID;
    //alert (dataString);return false;
    $.ajax({
      type: "POST",
      url: "#",
      data: dataString,
      success: function() {
        $("#groupOptions").html(dataString);
      }
    });
    return false;
});

Then the ajax goes to a php call that gets the options that match the groups id in the database. 然后,ajax转到php调用,该调用获取与数据库中的组ID匹配的选项。

if(isset($_POST['GroupID']))
{
    $GroupID = $_POST['GroupID'];
    $sql = "SELECT * from `KC_Options` WHERE GroupID=$GroupID";

    $GroupOptions = $db->query($sql);
}

Now I want to refresh the div #GroupOptions to display the results from the query above, and make <?php if($GroupOptions): ?> set to true. 现在,我想refresh div #GroupOptions以显示上面查询的结果,并将<?php if($GroupOptions): ?>设置为true。

I managed to refresh the div with $("#groupOptions").html(dataString); 我设法用$("#groupOptions").html(dataString);刷新了div $("#groupOptions").html(dataString); in the success function of the ajax call. 在ajax调用的成功函数中。 But that only returns well the dataString. 但这只会很好地返回dataString。 (obviously). (明显)。 Is there a way to truly refresh just the div. 有没有一种方法可以真正刷新div。 Or a way to pass the info from the php call into the success function? 还是一种将信息从php调用传递到成功函数的方法?

UPDATE: 更新:

You have 4 problems in your current code: 您当前的代码中有4个问题:

Problem #1 and Problem #2 - In your separate PHP script you are not echoing anything back to the Ajax. 问题1和问题2-在单独的PHP脚本中,您没有将任何内容回显到Ajax。 Anything you echo will go back as a variable to the success function. 您回显的任何内容都将作为success函数的变量返回。 Simply the add echo statement(s) according to the format you want. 只需根据您想要的格式添加echo语句即可。 Your 2nd problem is that you are trying to echo it in the HTML part, where $GroupOptions does not even exist (the Ajax simply returns an output from the PHP script, it's not an include statement so your variables are not in the same scope). 第二个问题是您试图在HTML部分中回显它,甚至$GroupOptions都不存在(Ajax只是从PHP脚本返回输出,它不是include语句,因此您的变量不在同一范围内) 。

if(isset($_POST['GroupID']))
{
    $GroupID = $_POST['GroupID'];
    $sql = "SELECT * from `KC_Options` WHERE GroupID=$GroupID";

    $GroupOptions = $db->query($sql);

    //this is where you want to iterate through the result and echo it (will be sent as it to the success function as a variable)
    if($GroupOptions): 
      foreach ($GroupOptions as $optionValue): 
        echo $optionValue['optionName'];
      endforeach; 
    endif; 
}

In your Ajax, add a variable named data to the success function, which will receive the output from the PHP script. 在您的Ajax中,向成功函数添加一个名为data的变量,该变量将接收PHP脚本的输出。 Also notice that your url is incorrect, you need to post to an actual external file such as my_custom_script.php .: 另请注意,您的网址不正确,您需要发布到实际的外部文件,例如my_custom_script.php

$.ajax({
      type: "POST",
      url: "your_external_script.php",
      data: dataString,
      success: function(data) {
       if (data && data !== '') {
         //data will equal anything that you echo in the PHP script
         //we're adding the label to the html so you don't override it with the new output
         var output = '<label class="control-label">Group Options</label>';
         output += data;
         $("#groupOptions").html(output);
       } else {//nothing came back from the PHP script
         alert('no data received!');
       }
      }
    });

Problem #4 - And on your HTML, no need to run any PHP. 问题4-在您的HTML上,无需运行任何PHP。 Simply change: 只需更改:

<div class="col-lg-6" id="groupOptions">
        <label class="control-label">Group Options</label>
        <?php if($GroupOptions): ?>
        <?php foreach ($GroupOptions as $optionValue): ?>
        <?php echo $optionValue['optionName']; ?> <?php endforeach; ?>
        <?php endif; ?>
    </div>

to

<div class="col-lg-6" id="groupOptions">

</div>

Hope this helps 希望这可以帮助

You have to take the response in yout success callback function and actually give a response in your oho function 您必须在成功回调函数中接受响应,并在oho函数中实际给出响应

$("#productOptions").change(function(){ $(“#productOptions”)。change(function(){

    var GroupID = $(this).val();
    var dataString = 'GroupID=' + GroupID;
    //alert (dataString);return false;
    $.ajax({
      type: "POST",
      url: "#",
      data: dataString,
      success: function(dataString) {        //take the response here
        // convert dataString to html...
        $("#groupOptions").html(newHtml);
      }
    });
    return false;
});

PHP: PHP:

if(isset($_POST['GroupID']))
{
    $GroupID = $_POST['GroupID'];
    $sql = "SELECT * from `KC_Options` WHERE GroupID=$GroupID";

    $GroupOptions = $db->query($sql);
    echo json_encode($GroupOptions );         //give a response here using json
}

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

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