dongteng0748 2013-12-18 13:01
浏览 48

使用jquery将Php变量发送到php脚本

I am trying to send a variable with jquery to a php script now this is my first try after reading the documentation but it wont work. can anyone give me some advice ? I didn't realize the error in code. The variable I want to send is $id. I defined it with '1'

<?php
$id = '1';
?>

jquery part

<script>
$(document).ready(function() {    // Anfang Dokument Ready Funktion
$("p").click(function () {  // Anfang Klick Funktion
 var id = $(this).html();    // Inhalt in Klickfunktion
// Beginn Ajax Segment mit Angabe von Typ Url Data und Div #ausgabe
    $.ajax({
        type: 'POST',
        url: 'http://localhost/ajax.php',
        data: 'id='+id',
        success: function(data){
            $('#ausgabe').html(data);
        }
        });     // Ende Ajax Segment 
});    // Ende Klick Funktion
});  // Ende Dokument Ready Funktion
</script>

<div id="ausgabe"></div>
<p><button>Sende Variable</button></p>

The ajax.php

<?php
$id = $_POST['id'];
echo $id;
?>
  • 写回答

3条回答 默认 最新

  • dpfl37651 2013-12-18 13:05
    关注

    The right form to send info with ajax to PHP is:

    $.ajax({
            type: 'POST',
            url: 'http://localhost/ajax.php',
            data: id:id,
            success: function(data){
                $('#ausgabe').html(data);
            }
            }); 
    

    To send by reference. If you want to send without reference just use:

    $.ajax({
            type: 'POST',
            url: 'http://localhost/ajax.php',
            data: id,
            success: function(data){
                $('#ausgabe').html(data);
            }
            }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么