dpict99695329 2013-07-17 16:06 采纳率: 100%
浏览 29

我尝试将AJAX变量混合到PHP时出现问题

So I'm trying to make the simplest of communications between AJAX and PHP, and send a single variable to my PHP, where it will be stored, but nothing seems to work u_u

This here is my AJAX code

$(document).ready(function() {

 $("#ch1").click(function(){

  document.getElementById('stf').style.visibility="visible";
  document.getElementById('ch2').style.visibility="visible";
  document.getElementById('reto1').src="slides/reto1_completo.png";
  var counter = 1;
  var longform = $("input:text").serialize();
  $.ajax({
   type: GET,
   url: 'counter.php',
   data:   longform + "counter=<?php echo $counter; ?>",
  })
 })
})

And this one here is my PHP

session_start();

$db = mysql_connect("localhost","db","pass");
if(!$db) die("Error");
mysql_select_db("db",$db);

$counter = $_GET['counter'];

$insert = mysql_query("UPDATE usuarios SET retos='$counter' WHERE email = '$correo'") or die(mysql_error());

So this is it, I'm sure it's the simplest of mistakes, but I just can't figure it out. Thanks in advance

  • 写回答

3条回答 默认 最新

  • douyi1982 2013-07-17 16:08
    关注

    You're building your data string wrong. longform will look something like:

    foo=bar&baz=qux
    

    which you then tack on the coutner value directly:

    foo=bar&baz=quxcounter=$counter
    

    You need a & separator:

       data:   longform + "&counter=<?php echo $counter; ?>",
                           ^---here
    

    so counter will be its own key, not part of the baz value that's at the end of longform.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化