Home » Php » Sending data from front-end to back-end to front-end

Sending data from front-end to back-end to front-end

Posted by: admin October 21, 2018 Leave a comment

Questions:

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>
Answers: