AJAX doesn't send data, I can't use POST Result

问题: EDIT : I would like to have a button value in another page. I've think to use Ajax, but I dn"t know if it's the best way.. Here is my Form: <form id="form_prj"&gt...

问题:

EDIT :

I would like to have a button value in another page. I've think to use Ajax, but I dn"t know if it's the best way..

Here is my Form:

<form id="form_prj">
    <button class="btn_prj" type="button" name="var_prj" value=60>Afficher</button>
</form>

Here is my Ajax: projet.php

$(document).ready(function() {
        $(document).on('click', '.btn_prj', function() {
            var sendData = {
                lid: this.value
            };

            $.ajax({
                type: 'POST',
                url: '../pit_app/carte.php',
                cache: false,
                data: {
                    lid: sendData,

                },
                success: function(data) {
                    console.log(data);
                    window.location = './carte.php';
                },
                error: function() {
                    alert("Error Occured");
                }
            });
        });

Here get_data.php

<?php
include('fonctions.php');
$connect = connect();
if(isset($_POST["lid"])) {
    $btn_value = $_POST['lid'];
}else{
    echo ("Error");
}
$sql1 = "SELECT row_to_json(fc)FROM (
    SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features 
    FROM (
    SELECT 'Feature' As type, ST_AsGeoJSON(ST_transform(ST_SetSRID(geom, 2154), 
    4326))::json As geometry, row_to_json(n) As properties 
    FROM chambre As n,projet WHERE ST_Contains(zone_etude,geom) and id_projet=" . $btn_value . ") As f)  As fc;";

$result = pg_query($connect, $sql1);
$rs = pg_fetch_array($result);
$legeojson = $rs[0];
?>

Error screen

EDIT2:

Ok, I added my query to the isset and now I have another problem. In my third file, carte.php where I included get_data.php

I have var geojson = </ br> But I need

var geojson = <?php echo $legeojson;?>;

Notice: Undefined variable: legeojson in E:wampwwwstage_axianspit_appcarte.php on line 108


回答1:

I've made some small clean up of your code. Your click action was a bit wrong, to get the button instance by its name you can usee $('[name="var_prj"]') which mean that $(this) will return exactly this button in the body of the click event

Next, to get the value from the button value attribute you can use $(this).attr('value') if you want to get value from input tags you can use simple $(this).val().

And the last part is a data parameter of your AJAX call. If you already have an object with key and value, just put to the data param of the $.ajax

<form id="form_prj">
    <button type="button" name="var_prj" value=60>Afficher</button>
</form>
$(document).ready(function () {
        $('[name="var_prj"]').click(function () {
            var sendData = {
                lid: $(this).attr('value'),
            };

            $.ajax({
                type: 'POST',
                url: '../pit_app/carte.php',
                cache: false,
                data: sendData,
                success: function (data) {
                    window.location = './carte.php';
                },
                error: function () {
                    alert('Error Occured');
                },
            });
        });
    });

Here is the $_POST array of your carte.php after sending AJAX request.

Array
(
    [lid] => 60
)

Hope my answer would help to you

  • 发表于 2019-03-01 19:00
  • 阅读 ( 272 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除