weixin_33737134 2011-09-23 09:20 采纳率: 0%
浏览 28

简单的jQuery和PHP问题

Does anyone know why this doesn't work? any help would be appreciated.

This is the HTML

Enter Code:<br/><input type="text" name="code" id="code"/><br/> 
<input type="button" id="confirm" value="Confirm" onClick="confirm()"/> 

This is the PHP (basically gets the value of the user input and if it's equal to the variable a echo sucess)

$code = $_POST["code"]; 
$a = 105678; 
if($code==$a){ 
  echo "sucess"; 
} else { 
  echo "nosucces"; 
} 

The JavaScript (simple ajax code to alert yay on PHP sucess or nay on no sucess)

function confirm () { 
  $.post(
    "confirm.php",
    {code:$(this).val() },
    function(data) { 
      if(data=='sucess') { 
        alert("yay"); 
      } else { 
        alert("nay");
      } 
    }
  ); 
}

basically on all occasions the ouput is nay on further debugging i tried an elseif statement on the javascript asking if data is equal to nosucces but it didn't even alert anything, meaning it's not getting the data from the php

  • 写回答

3条回答 默认 最新

  • weixin_33737774 2011-09-23 09:31
    关注

    Give brackets at end, so that it does understand that you are calling a function, also don't use confirm as function name, its a native JS function, use my_confirm instead.

    onClick="my_confirm()"
    

    Also $(this).val() won't work in your case use $('#code').val() instead.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码