Я - два файла, файл PHP и другой файл из javascript и HTML.Отображать предупреждение при использовании AJAX
PHP файл:
<?php
session_start(); //start session
$_SESSION['data'] = "The content should be displayed as alert in the JS file";
JS/HTML файла:
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<form class="form-item" id="idForm">
<input type="hidden" name="product_code" value="prince"/>
<button id="btn_add" type="submit">Add to Cart</button>
</form>
<script>
$(function(){
$("#idForm").submit(function(e) {
var url = "test2.php";
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(),
success: function()
{
alert(/*The content of the session in the php file should be display here*/); // show response from the php script.
}
});
e.preventDefault();
});
});
</script>
В файле PHP, у меня есть сеанс $_SESSION['data']
. Все, что я хочу сделать, это возможность отображать содержимое этой сессии в alert()
после making the ajax request to test2.php
в 'php' write' echo $ _SESSION ['data']; ', а затем в' ajax': -'success: function (response) {alert (response);} ' –
@Anant done. Извините за задержку – Prince