duanbin4847 2017-03-12 21:29
浏览 45

从php中的ajax获取数据

I'm new to ajax and I'm stuck in a problem. I want to post the id of the clicked element in a result list to another page (in localhost). When I want to get the posted id from the ajax data I get undefined index id (which is $id = $_POST['id']). how can I solve this ?

I appreciate any help or advice.

index.php

<section id= "searchbar">
        <form method="get" action="index.php" name="myForm" id="myForm">
            <h3>Search</h3>
        <table>
            <tr>
                <td><input id="wilaya" type="text" name="wilaya" class="resizedTextbox" placeholder="Choose your Wilaya"></td>
                <td><input id="surface" type="text" name="surface" class="resizedTextbox" placeholder="Choose the surface"></td>
                <td><input id="search" type="submit" class="resizedTextbox" value="Search"></td>
            </tr>

        </table>
    </form>
    </section>




     <section id="resultlist">
            <table width="100%" height="90%" id="table1">
                <thead>
                    <tr>
                    </tr>
                </thead>

                <?php
                $array[] = null;
                $i = 1;
                while ($results = mysql_fetch_assoc($result)) {
                    ?>
                    <tr>

                    <div class="round-button">
                        <!--echo '<img name= "house" class= "imagehouse" src="images/' . $results['photo'] . '" id=' . $results['id'] . ''-->   
                        <?php echo "<img src= images/" . $results['photo'] . " width='50' id=" . $results['id'] . " />"; ?>
                        <td id="nametable" width="10%"> <?php echo $results['name']; ?></td>
                        <td id="wilayatable"width="10%"><?php echo $results['wilaya']; ?></td>
                        <td width="10%"><?php echo $results['surface'] . " m²"; ?></td> 
                        </tr>
                        <?php
                        $i++;
                    }
                }
                ?>
        </table>
    </section>

    <?php
//extract data from the post
//set POST variables
        $url = 'http://localhost/testcurl.php';

        $fields = array(
            'wilaya' => urlencode($_POST['wilaya']),
            'id' => urlencode($_POST['id']),
        );

//url-ify the data for the POST
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');

//open connection
        $ch = curl_init();

//set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
        $result = curl_exec($ch);

//close connection
        curl_close($ch);
        ?>
    <?php
    var_dump($_POST['id']);
    ?>

and that's the function that get the id of the clicked image

        $('img').click(function () {
            alert(this.id);
            var id = this.id;
            $.ajax({
                url: 'index.php',
                type: 'post',
                data: {
                    'id': id
                },
                success: function (msg) {
                    alert('Success!');
                }
            });
            $.get("http://localhost/testcurl.php");
            window.location = 'http://localhost/testcurl.php';
        }); 

testcurl.php

here I send a post the id to the server

<?php

$url = "http://localhost/testcurlserver.php";// where you want to post data

$wilaya = isset($_POST['wilaya']);
$id = isset($_POST['id']);
$fields = array(
    'wilaya' => urlencode($_POST['wilaya']),
    'id' => urlencode($_POST['id']),

);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

var_dump($output); // show output

?>

testcurlserver.php here is the server code

<?php

if (isset($_POST['id'])) {

    $jj =$_POST['id'];
    var_dump($jj);

    echo "it worked";

    $wilaya = $_POST['wilaya'];
    // connect to database 
    $db = mysql_connect('localhost', 'root', 'test123') or die('error because:' . mysql_error());
    $mydb = mysql_select_db("top360");
    $sql = "SELECT id,photo,name, wilaya, surface FROM HOUSE WHERE wilaya= '" . $wilaya . "'";
    //-run the query against the mysql query function
    $result = mysql_query($sql);

    while ($row = mysql_fetch_array($result)) {
        echo $row['name'];
    }


    if ($result === FALSE) {
        die(mysql_error());
    }
    $results = mysql_fetch_assoc($result);


    echo $results['name'];
}

here is what I get:

Notice: Undefined index: id in C:\xampp\htdocs\index.php on line 122 
Notice: Undefined variable: fields_string in C:\xampp\htdocs\index.php on line 127 
  Notice: Undefined variable: fields_string in
  C:\xampp\htdocs\testcurl.php on line 14 string(0) "" it worked Notice:
  Undefined variable: output in C:\xampp\htdocs\testcurl.php on line 31
  NULL  
Notice: Undefined index: id in C:\xampp\htdocs\index.php on line
  147 NULL
  • 写回答

2条回答 默认 最新

  • 普通网友 2017-03-12 21:40
    关注

    please type at top of the your code

    if (! is_array($_POST)) {
       $_POST = json_decode(file_get_contents('php://input'), true);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上