Мне нужен AJAX для прослушивания, если нажата кнопка.Тогда, если это мне нужно, чтобы запустить скрипт PHP.У меня проблемы с тем, что AJAX неправильно слушает нажатие кнопки, и поэтому он никогда не запускает скрипт.Любая ошибка в моем коде вы можете увидеть?Любой совет, как мне это сделать?
Кнопка:
<input id="button_1" type="button" value="favorites1" onclick="favfunct();" />
AJAX, который вызывает его: (ajaxlisten.js
)
<script type="text/javascript">
$(document).ready(function () { // Make sure the elements are loaded on the page
// Listen for a click event on the button
$('#button_1').click(favfunct);
});
function favfunct(e) {
// Stop the page from "following" the button (ie. submitting the form)
e.preventDefault();
e.stopPropagation();
// Call an AJAX function to the proper page
$.ajax("js/addtofavorites.php", {
// Pass our data to the server
data: { "get" : "runfunction", "action" : "favorites1" },
// Pass using the appropriate method
method: "POST",
// When the request is completed and successful, run this code.
success: function (response) {
// Successfully added to favorites. JS code goes here for this condition.
alert ("successfully loaded")
}
});
}
</script>
Php файл(addtofavorites.php
)
<?php
$con = mysql_connect("localhost","root","student");
if ($_POST["action"] = 'favorites1')
{
if (!$con);
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tvid", $con);
$sql="INSERT INTO tv (userid, favorites) VALUES (345,77);"
if (!mysql_query($sql,$con));
{
die('Error: ' . mysql_error());
}
echo "Your Video was Added To Your Favorites";
mysql_close($con);
}
?>