1

I wanted to get the data from the ajax post which are the student name and student religion(abdullah and muslim) values in the passwrapper.php and post those values on the console.log. However, i cant find the posted values on the console.log. i want to show those posted values on the console.log.

Here is my code below.... orignially my code is about performing ajax post to the passwrapper.php and then the passwrapper.php include another script, student.php to show all the data on the html file.

HTML FIle

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        contentType: "application/json",
        dataType: "json",
        data: {
            lastName: 'Abdullah',
            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>

Passwrapper.php

    <?php
include 'student.php';
executePass();

receivePost();
function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    } 
    else 
    {
        echo '<script>console.log("Firstname='.$_POST["lastName"].' lastReligion='.$_POST["lastReligion"].'");</script>';

    }
}

?>

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);
}
?>

my question is how to post those values abdullah and muslim on the console.

  • Try this https://www.google.co.za/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/4323411/how-can-i-write-to-console-in-php&ved=0ahUKEwjJ7svolO_VAhVhAcAKHQXhBJ4QFggmMAA&usg=AFQjCNGV77WLBpmad3zBII_ygJU7g0J4KA – Jephren Naicker Aug 24 '17 at 05:46
  • Possible duplicate of [How can I write to console in PHP?](https://stackoverflow.com/questions/4323411/how-can-i-write-to-console-in-php) – Jephren Naicker Aug 24 '17 at 05:47

1 Answers1

0

try this,

<?php
include 'student.php';
executePass();

if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
{
    //do nothing
} 
else 
{
    //post the values(Abdullah and Muslim) on the console.log on the passwrapper.php
    //i do not want to disrupt the executepass()

echo '<script>console.log("Firstname='.$_POST["lastName"].' lastReligion='.$_POST["lastReligion"].'");</script>';

}
?>
  • 1
    about ur code, i saw the post data on the html page console. i receive the error which states ''Status Code: 200 ErrorThrown: SyntaxError: Unexpected token < in JSON at position 1634 jqXHR.responseText: all the data is posted here.. –  Aug 24 '17 at 06:03
  • i want the posted data on the passwrapper.php console log not on the html file –  Aug 24 '17 at 06:05
  • is it possible not to disrupt the ajax process?? –  Aug 24 '17 at 06:18
  • it's posting on html file because on this ` echo json_encode($json_array);` in the executePass function. – Nicholas Snr Aug 24 '17 at 06:49
  • To post on console instead of html kindly use **echo '';** in place of echo json_encode($json_array); – Nicholas Snr Aug 24 '17 at 07:23
  • If that doesn't help, kindly explain exactly what you want to achieve so that we can understand where you're getting it wrong – Nicholas Snr Aug 24 '17 at 07:24
  • ok so basically, the json data(all the data) are posted on the html console page. I want the ajax post value which are Abdullah and Muslim to be posted on the console log as well... –  Aug 24 '17 at 07:55
  • about the ajax post data, can those two data be posted on the console.log.. Which one can it be posted?? can it be on the passwrapper.php or html file –  Aug 24 '17 at 08:03
  • i have updated my code and you can see the code .. There is no error and all the data is posted on the html console. however there is no ajax post data.. i cant find the ajax post data on the html file... –  Aug 24 '17 at 08:13
  • there is no error because i implemented contenttype:"application/json".. what is the purpose of this?? –  Aug 24 '17 at 08:35
  • should i implement $.post in the ajax –  Aug 25 '17 at 02:02