1

i was asked to perform ajax post some values to the php script. when the php script receive the values and should show the values. However the values was not shown on the console.log. WHERE ARE YOU AJAX POSTED VALUES?? MY code is found below.... where will the post values be posted?? will it be posted on the html file or php file???

student.php

<?php
function executePass()
{

    $conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
    $db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

    $result = mysqli_query($conn,"select * from student");
    $json_array = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $json_array[] = $row;
    }

    echo json_encode($json_array);
}
?>

passwrapper.php

<?php
include 'student.php';
executePass();
receivePost();
function receivePost()
{
    if ((!isset($_REQUEST["lastName"])) and (!isset($_REQUEST["lastReligion"])))
    {
        //do nothing
    } 
    else 
    {
        echo '<script>console.log("LastRowname = '.$_POST["lastName"].' LastRowReligion = '.$_POST["lastReligion"].'");</script>';
    }
}
?>

html file

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="results"</div>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        contentType: "application/json",
        dataType: "json",
        data: {
            lastName: 'Abdullahlahlahlah',
            lastReligion: 'Muslim',
        },      
        success: function(data){
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
        },

    });
};
</script>
</body>
</html>

originally my code is explained below.. i perform ajax post to the passwrapper.php and this script include another script to show all data on the html console page. I was asked to perform an ajax post to post some values and get those values and post the values on the console.log which is on the html and the passwrapper.php..the whole data was posted on the html console however I was not able to find the posted values which are Abdullahlahlahlah and Muslim on the console.log.. Can you help me on how to show the posted values on the console.log in the passwrapper.php.. thank you.....

  • Possible duplicate of [ajax post values is empty](https://stackoverflow.com/questions/45853676/ajax-post-values-is-empty) – Jack jdeoel Aug 24 '17 at 09:35
  • @DavidJorHpan yeah my code is updated –  Aug 24 '17 at 09:38
  • Is that your need ? Take a look this please https://stackoverflow.com/questions/6763148/how-to-show-literal-html-script-in-a-web-page – Jack jdeoel Aug 24 '17 at 09:40
  • @David JorHpan that is not related to my questions... My question is that i am not able to see the ajax posted data values on the console.log.... –  Aug 24 '17 at 09:44
  • If you want to debug posted data in php , you need to use `var_dump` or `print_r` . `console.log` is javascript debugging !!! – Jack jdeoel Aug 24 '17 at 09:51
  • @David JorHpan anyway he should get message at console according to `echo ''; ` and `console.log(data);` – diavolic Aug 24 '17 at 09:53
  • @diavolic, that is right, but why the posted data is not shown in the console.log?? –  Aug 24 '17 at 09:56
  • @Tariq because you asked server to return json data, but try to output it in console as a string ))) – diavolic Aug 24 '17 at 09:59
  • replace `contentType: "application/json", dataType: "json", data: { lastName: 'Abdullahlahlahlah', lastReligion: 'Muslim', }, ` to `data: "lastName=Abdullahlahlahlah&lastReligion=Muslim,` and try again – diavolic Aug 24 '17 at 10:01
  • @diavolic, does that mean if i json _encode the $post and show in json. will you be able to see it??? –  Aug 24 '17 at 10:01
  • @diavolic u mean like this data: "lastName=Abdullahlahlahlah&lastReligion=Muslim", –  Aug 24 '17 at 10:05
  • @diavolic, the result fail as i am not able to see the posted data –  Aug 24 '17 at 10:06
  • how about remove `contentType: "application/json"` only – Jack jdeoel Aug 24 '17 at 10:10
  • if i remove that, there is error –  Aug 24 '17 at 11:34
  • is there any way???? –  Aug 24 '17 at 13:11

0 Answers0