最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending data from front-end to back-end to front-end - Stack Overflow

programmeradmin2浏览0评论

Hi i'm a beginner in using JavaScript i have this html page with JavaScript codes that receives data from the server and display it on this current page, what i'm trying to do is use that data and sending it to another PHP page for my SQL query to get back results.

<script>
    var json = sessionStorage.xhr;
    var object = JSON.parse(json);
    var hard =  object["red-fruits"];
    var string = JSON.stringify (hard);
    var stringData = encodeURIComponent(string);
    $.ajax({
        type: "POST",
        url: "http://localhost/web/main.php",
        data: {"dataA" : stringData}, 
        cache: false,
            success: function(){
                console.log("OK");
            }
    });
    var user = sessionStorage.getItem('impData');
    console.log(user);
</script>

This is my PHP page codes, what i'm doing here is getting the data "dataA" from that html page and sending it to this PHP page for the SQL query and getting the results which is the "$haha" array and using JavaScript session function to send it back to the HTML page. But my console only shows "null" can anyone tell me if i'm doing anything wrong or have any suggestion would be really appreciated.

<?php

$connection = mysqli_connect("localhost","root","","") or 
die("Error " . mysqli_error($connection));
    if (isset($_POST['dataA'])) {
        echo $name = $_POST['dataA'];
    }
    else {
        echo "Error";
    }

$string = str_replace("]", "", str_replace("[", "", str_replace('"','',$falcon)));
$array = explode(',', $string);
$array2= implode("', '",$array);

$sql = // "SQL query"
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . 
mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {
    $haha[] =  $row['row_name'];
}

?>

<script type="text/javascript">
    var tills = <?php echo '["' . implode('", "', $haha) . '"]' ?>;
    console.log (tills);
    sessionStorage.setItem('impData', tills);
</script>

Hi i'm a beginner in using JavaScript i have this html page with JavaScript codes that receives data from the server and display it on this current page, what i'm trying to do is use that data and sending it to another PHP page for my SQL query to get back results.

<script>
    var json = sessionStorage.xhr;
    var object = JSON.parse(json);
    var hard =  object["red-fruits"];
    var string = JSON.stringify (hard);
    var stringData = encodeURIComponent(string);
    $.ajax({
        type: "POST",
        url: "http://localhost/web/main.php",
        data: {"dataA" : stringData}, 
        cache: false,
            success: function(){
                console.log("OK");
            }
    });
    var user = sessionStorage.getItem('impData');
    console.log(user);
</script>

This is my PHP page codes, what i'm doing here is getting the data "dataA" from that html page and sending it to this PHP page for the SQL query and getting the results which is the "$haha" array and using JavaScript session function to send it back to the HTML page. But my console only shows "null" can anyone tell me if i'm doing anything wrong or have any suggestion would be really appreciated.

<?php

$connection = mysqli_connect("localhost","root","","") or 
die("Error " . mysqli_error($connection));
    if (isset($_POST['dataA'])) {
        echo $name = $_POST['dataA'];
    }
    else {
        echo "Error";
    }

$string = str_replace("]", "", str_replace("[", "", str_replace('"','',$falcon)));
$array = explode(',', $string);
$array2= implode("', '",$array);

$sql = // "SQL query"
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . 
mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {
    $haha[] =  $row['row_name'];
}

?>

<script type="text/javascript">
    var tills = <?php echo '["' . implode('", "', $haha) . '"]' ?>;
    console.log (tills);
    sessionStorage.setItem('impData', tills);
</script>
Share Improve this question edited May 24, 2018 at 5:40 Krunal Shah 8631 gold badge8 silver badges25 bronze badges asked May 24, 2018 at 5:27 Best JeanistBest Jeanist 1,1294 gold badges14 silver badges37 bronze badges 1
  • Don't mix PHP and JavaScript. Use hidden inputs to pass data from Php to JavaScript – treyBake Commented May 24, 2018 at 6:13
Add a ment  | 

1 Answer 1

Reset to default 2

You are now mixing ajax and session data on a strange way. The session data used by your javascript will not be updated by the php-script till you refresh your page. The correct way to handle data is in the "success" function:

$.ajax({
        type: "POST",
        url: "http://localhost/web/main.php",
        data: {"dataA" : stringData},
        dataType : "json",
        cache: false,
            success: function(data){
                console.log(data);
            }
    });

and in you PHP output the data you want to send to the browser as a json string:

echo json_encode($your_object);
发布评论

评论列表(0)

  1. 暂无评论