Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajaxjquerymysqli更新_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript ajaxjquerymysqli更新

Javascript ajaxjquerymysqli更新,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图做一个Ajax演示来教自己这个概念,但我就是不能让它工作 我尝试获取的示例是在第一个页面上获取一个变量(在本例中为绿色),如果单击了绿色按钮,则在ajax处理页面上使用该变量以某种方式记录绿色按钮被单击(当前在错误日志中,然后我将把它存储在数据库中) 理想情况下,我希望能够重用相同的代码段,将变量更改为红色,并让错误日志报告按下了红色按钮 我使用location.reload和随机数向自己证明javascript正在执行,并且页面随着每次加载而不同 我根本不是一个javascript的家伙

我试图做一个Ajax演示来教自己这个概念,但我就是不能让它工作

我尝试获取的示例是在第一个页面上获取一个变量(在本例中为绿色),如果单击了绿色按钮,则在ajax处理页面上使用该变量以某种方式记录绿色按钮被单击(当前在错误日志中,然后我将把它存储在数据库中)

理想情况下,我希望能够重用相同的代码段,将变量更改为红色,并让错误日志报告按下了红色按钮

我使用location.reload和随机数向自己证明javascript正在执行,并且页面随着每次加载而不同

我根本不是一个javascript的家伙,你可能知道。我需要先声明一个变量还是完全其他什么

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>";

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

功能单击是(a){
$.ajax({
类型:“POST”,
url:“./ajax.php”,
数据:{“测试”:a},
成功:功能(响应){
默认值=$(“#默认值”).val();
});
location.reload();
}
函数clickno(){
location.reload();
}

此处
位置。由于ajax是一个异步函数,因此将在不等待ajax请求的情况下调用reload
。这可能是问题所在。在ajax响应成功时保持reload

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

}


Also Change this: 
echo "<button onclick=\"clickyes('" . $result . "')\" style=\"background-color: $result; \">Yes</button>";
功能单击是(a){
$.ajax({
类型:“POST”,
url:“./ajax.php”,
数据:{“测试”:a},
成功:功能(响应){
默认值=$(“#默认值”).val();
location.reload();
});
}
还要改变这一点:
回应“是”;
此处更改:

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

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>";
echo“这里有一个$result按钮。你想点击它吗?

”; 回应“是”; 回应“否”;
成功了。万岁!


<script src="https://code.jquery.com/jquery-2.2.3.js"></script>
<form><p id="color-info">here's a  button. Do you want click on it?</p>
    <button color="" id='yesbutton'>Yes</button>
    <button >No</button></form>

<script type="text/javascript">
setColor();
// this code will be invoked when #yesbutton is clicked and this is called dedication method
$(document).on('click','#yesbutton',function(e){
  e.preventDefault();
  $.ajax
  ({
   type: "POST",
   url: "./ajax.php",
   data: {"test":$('#yesbutton').attr('color')},
   success: function (response) {
      //default = $("#default").val();
      //change color after success
      setColor();

   }


   });

});

// function first determine rgb color to create composite color
function setColor()
{
  //redcolor code
  $redcolor=Math.random()*255;
  // green color code
  $greencolor=Math.random()*255;

  //blue color code
  $bluecolor=Math.random()*255;
  //this will set the background color for first time
  $rgb=parseInt($redcolor)+','+parseInt($greencolor)+','+parseInt($bluecolor);
  $('#color-info').text("here's a rgb("+$rgb+") button. Do you want click on it?");
  $('#yesbutton').css("background",'rgb('+$rgb+')');
  //this will set the color attribute for first time
  $('#yesbutton').attr('color',$rgb);


}

</script>

这里有一个按钮。你想点击它吗

对 不 setColor(); //单击#yes按钮时将调用此代码,这称为奉献方法 $(文档).on('click','yesbutton',函数(e){ e、 预防默认值(); $.ajax ({ 类型:“POST”, url:“./ajax.php”, 数据:{“test”:$('yesbutton').attr('color')}, 成功:功能(响应){ //默认值=$(“#默认值”).val(); //成功后变色 setColor(); } }); }); //函数首先确定rgb颜色以创建合成颜色 函数setColor() { //红色代码 $redcolor=Math.random()*255; //绿色色码 $greencolor=Math.random()*255; //蓝色代码 $bluecolor=Math.random()*255; //这将首次设置背景色 $rgb=parseInt($redcolor)+'、'+parseInt($greencolor)+'、'+parseInt($bluecolor); $(“#颜色信息”).text(“这是一个rgb(“+$rgb+”)按钮。要单击它吗?”); $('yesbutton').css(“背景”,“rgb(+$rgb+)”); //这将首次设置颜色属性 $('yesbutton').attr('color',$rgb); }
如果有人需要继续重新加载,为什么要使用ajax@VeshrajJoshi…只是一个实验。如果我用随机数来改变按钮的颜色,然后用ajax代码来记录被按下的颜色,会怎么样;