Zahid Jabbar Zahid Jabbar - 1 month ago 8
Ajax Question

call value of a variable from php page by ajax

I am new to javascript and I've searched this question everywhere but no suitable answer.
I have a "code.php" page as

$query = "SELECT * FROM users WHERE id=1";
$result= mysqli_query($con,$query);
$row=mysqli_fetch_array($result);
echo $row[1];


On the other side in "index.php" i have

<h1>"the value for number is: " <span id="myText"></span></h1>

function myFunction() {
document.getElementById("myText").innerHTML = $row[1];
}


I want to load the value of
$row[1]
from
code.php
to
index.php
.

I know that i have to use ajax but how?

Answer Source

In index php use ajax to get data from code php. this is example :

<script>
  $.ajax({
    url: 'code.php',
    type:'get',
    success:function(data){
      $("#myText").html(data);
    }
  });
</script>