I'm using AJAX to transport id name of my div to use it as a string in a query. The problem is that it does not want to display any content related to the database (it can display a normal text echo from my function).
// head HTML (AJAX)
$(function () {
$('.product').on('click', function (e) {
var product = $(this).attr('id');
$.ajax({
type: "post",
url: 'php/recipe-container.php',
data: JSON.stringify({
data: product,
}),
processData:false,
processData:false,
contentType: false,
success: function(response) {
$(".display_recipe").html(response);
}
});
});
});
//HTML
<div class="product" id="pizza">pizza</div>
<div class="product" id="lasagna">lasagna</div>
<div class="product" id="sushi">sushi</div>
<div class="display_recipe">
<?php
require "php/recipe-container.php";
?>
</div>
//PHP (recipe-container.php)
<?php
function display(){
$con = mysqli_connect("localhost", "root", "", "cookbook");
echo "test ";
$product = substr(file_get_contents("php://input"), 9, -2);
$sql = "SELECT * FROM product WHERE name = '$product'";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)) {
echo "test2 ";
$name = $row['name'];
$description = $row['description'];
$date = $row['date'];
echo $name;
echo $description;
echo $date;
}
mysqli_close($con);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
display();
}
?>
This is what I see after the button click - "test test2 pizza". "pizza" is the ID name, not the actual name from the database. I don't know how to remove that. I know everything has something to do with the "success" function (response), but I have no idea how to fix that.
My Answer:
JavaScript and PHP are both popular programming languages used for web development, but they serve different purposes and have different strengths.
JavaScript is a client-side scripting language that is used to create interactive and dynamic web pages. It is primarily used for front-end development, such as creating animations, form validation, and interactive elements on a website. JavaScript runs in the user's browser, so it can be used to manipulate the content of a webpage without needing to reload the entire page.
PHP, on the other hand, is a server-side scripting language that is used to create dynamic web pages and web applications. It is often used in conjunction with a database to create dynamic content, such as user authentication, content management systems, and e-commerce websites. PHP runs on the server, so it is used to generate HTML content that is then sent to the user's browser.
In summary, JavaScript is used for front-end development to create interactive and dynamic web pages, while PHP is used for server-side development to create dynamic web applications. Both languages have their own strengths and are often used together in web development projects.
Rate this post
4 of 5 based on 1242 votesComments