douhuan1937 2016-04-21 08:08 采纳率: 0%
浏览 61
已采纳

Ajax jquery mysqli更新

I'm trying to do a Ajax demo to teach myself the concept but I just can't make it work.

The sample I'm trying to get is to take a variable on the first page (in this case, the colour green) and if the green button is clicked, use that on the ajax processing page to record somehow that the green button was clicked (currently in the error log, then I'll move onto storing it in a database).

Ideally I'd like to be able to reuse the same snippet to change the variable to red, and have the error log report that the red button was pressed.

I've used the location.reload and the random number to prove to myself that the javascript is executing, and that the page is different with each load.

I'm not a javascript guy at all, you may be able to tell. Do I need to declare the a variable first or something else entirely?

uglytest.php

<script src="https://code.jquery.com/jquery-2.2.3.js"></script>
<!-- jQuery Ajax -->
<script type="text/javascript">
function clickyes(a) {
$.ajax({
       type: "POST",
       url: "./ajax.php",
       data: {"test":a},
       success: function (response) {
       default = $("#default").val();
       });
    location.reload();
}
</script>

<script type="text/javascript">
function clickno() {
    location.reload();
}
</script>


<?
echo mt_rand(0,99);
$result = "green"; 

echo "<p><form>here's a $result button. Do you want click on it?</p>";
echo "<button onclick=\"clickyes(" . $result . ")\" style=\"background-color: $result; \">Yes</button>";
echo "<button onclick=\"clickno()\">No</button></form>";

?>

ajax.php

<?
  $test = $_POST['test'];
   error_log($test, 0);
?>
  • 写回答

3条回答 默认 最新

  • drfkl66684 2016-04-21 10:48
    关注

    changes here:

    function clickyes(a) {
    $.ajax({
           type: "POST",
           url: "./ajax.php",
            data: {test:a},
           success: function () {
               location.reload();
           }
           });
    }
    

    and here:

    echo "<p><form>here's a $result button. Do you want click on it?</p>";
    echo "<button onclick=\"clickyes('$result')\" style=\"background-color: $result; \">Yes</button>";
    echo "<button onclick=\"clickno('$result')\">No</button></form>";
    

    made it work. Hooray!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号