I want to take some values from a PHP array to a HTML page by clicking a button into a paragraph ( <p>
)
How can I do that?
<?php //file called 2.2.php
$numbers = array(1, 8, 11, 26);
echo json_encode($numbers);
?>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type:"POST",
dataType: "json",
url: "2.2.php",
data: "data",
success:function(data){
//WHAT TO WRITE HERE?
},error:function(data){
alert("bla");
}
});
});
});
</script>
Parse the data from PHP (which will be text) to JSON, using:
JSON.parse( data )
THANKS FOR YOUR ANSWERS. BUT I DID IT: HERE IS FULL CODE:
php part:
<?php
if($_POST['data']){
$numbers = array(1, 8, 11, 26);
$arr = json_encode($numbers);
exit($arr);
}
?>
and script:
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type:"POST",
url: "2.2.php",
data: "data=1",
success:function(data){
$("#here").html(data);
},error:function(data){
alert("not working");
}
});
});
});
</script>
and html paragraph and button:
<body>
<button id="button">CLICK ME</button>
<p id="here"></p>
</body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.