Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Data not put into Paragraph JS/AJAX/PHP

My php script echoes

Connection successful{"name":"James","dateOfBirth":"2002-10-10","companyname":"Apple"}

using

echo json_encode(array('name'=>$row['Name'], 'dateOfBirth'=>$row['dateOfBirth'], 'companyname'=>$row['companyname']));

In Javascript the different values should then be displayed in a Label, but the Labels return undefined

$(function() {

    $.ajax({
     type: "post",
     url: 'url',
     data: {usermail: 42},
     success: function(response){

         nameLabel.innerHTML = response.name;
         dateLabel.innerHTML = response.dateOfBirth;
         companyLabel.innerHTML = response.companyname;
         console.log(response);

     }
 });

});

That is the php code:

<?php

$host = '';
$database = '';
$user = '';
$pswd = '';

header("Access-Control-Allow-Origin: http://127.0.0.1:5500");


$conn = new mysqli($host, $user, $pswd, $database);

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);
} 

echo "Connection successful";

$user = $_POST['usermail'];


$sql = $conn->prepare("SELECT Name, dateOfBirth, companyname FROM Customers WHERE user_id = ?");


$sql->bind_param("i", $user);
$sql->execute();

$result = $sql->get_result();

if($result->num_rows === 0) exit('No rows');

while($row = $result->fetch_assoc()) {

echo json_encode(array('name'=>$row['Name'], 'dateOfBirth'=>$row['dateOfBirth'], 'companyname'=>$row['companyname']));


}

$sql->close();
$conn->close();

?>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Remove the line echo "Connection successful"; from your PHP code.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!