Hi I would like to scroll to a div after running a php code that runs when i press the it searches the database and posts to a table thats located in that div
<?php
session_start();
include_once 'includes/dbh.php';
if(isset($_POST['search']))
{
$searchvalue = $_POST['searchvalue'];
$query = "SELECT * FROM `users` WHERE CONCAT(`user_id`, `user_username`, `user_password`, `user_name`, `user_gender`, `user_age`, `user_contact`, `user_email`, `user_address`, `user_industry`, `user_findout`, `user_admin`) LIKE '%".$searchvalue."%'";
$result = mysqli_query($conn,$query);
echo"
<script type='text/javascript'>
$(document).ready(function () {
$('html,body').animate({
scrollTop: $('.graph').offset().top
},
'slow');
})
</script>";
else
{
$query = "SELECT * FROM `users`";
$result = mysqli_query($conn,$query);
}
?>
The session_start()
needs to be before any HTML
output so that means somewhere above the <!DOCTYPE html>
tag. The rest of the code should be in the body
of the document so you can run the script
.
Also, use the $( window ).load();
instead of $(document).ready();
to be sure that the whole HTML page is loaded including CSS
and javascript
files.
Here is the corrected code:
<?php
session_start();
include_once 'includes/dbh.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<?php
if(isset($_POST['search']))
{
// at least use some kind of escaping.. (Use prepared statements!)
$searchvalue = mysqli_real_eascape_string($conn, $_POST['searchvalue']);
$query = "SELECT * FROM `users` WHERE CONCAT(`user_id`, `user_username`, `user_password`, `user_name`, `user_gender`, `user_age`, `user_contact`, `user_email`, `user_address`, `user_industry`, `user_findout`, `user_admin`) LIKE '%".$searchvalue."%'";
$result = mysqli_query($conn,$query);
echo '<script type="text/javascript">
$( window ).load(function()
{
$("html,body").animate({
scrollTop: $(".graph").offset().top
},
"slow");
});
</script>';
else
{
$query = "SELECT * FROM `users`";
$result = mysqli_query($conn,$query);
}
?>
</body>
</html>
Note: To help make your application more secure, I would suggest looking into mysqli prepared statements
. Read on prepared statements: http://php.net/prepared-statements.