I want to show title variable in ajax
PHP
<?php
$title = "Test-1";
?>
Ajax
$(".btn").click(function () {
if ($(".main-input-box").val().length > 0) {
$(".btn").load("results.php", {ID: $(".main-input-box").val()}, function (data, result) {
alert($title);
});
}
});
thanks.............................
Long time ago, I wrote PHP... I'm not sure its your ans...
$(".btn").click(function () {
if ($(".main-input-box").val().length > 0) {
$(".btn").load("results.php", {ID: $(".main-input-box").val(), title: "<?php echo $title ?>"}, function (data, result) {
alert($title);
});
}
});
If you have your php code and JavaScript code in same file then you can access it in JavaScript as
var title = "<?= $title ?>";
If JavaScript is in separate js file then before loading that JavaScript file in php and after assigning value to $title add
Main.php
<?php
$title = "test";
?>
<script>
var title = "<?= $title ?>";
alert (title);
</script>
Then you can alert (title)
PHP runs on the server. Browser-side JavaScript runs in the browser. To communicate between them you have to send data over HTTP.
To include the contents of the variable in the response, echo it.
<?php
$title = "Test-1";
echo $title;
?>
The purpose of jQuery's load function is to make an Ajax request and put the response as the content of an element.
If you want to alert it, don't use load
. Use post
instead.
$.post("results.php", {ID: $(".main-input-box").val()}, function (data) {
alert(data);
});
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.