Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  176] [ 4]  / answers: 1 / hits: 16408  / 9 Years ago, thu, october 1, 2015, 12:00:00

Iam trying to do something fancy. I have successfully written code to show results from database when a small form is filled and submit button is clicked.The results are shown right under the form. But I feel it would be nice if the page is automatically scrolled down to the div containing the results for the filled form. I suppose i have to use jquery or ajax for that. Since I have no knowledge in them I searched the internet for copy pasting the code. But none of them works.



When the submit button is clicked,the page will reload to fetch results from database.I have got the code for scrolling down to a div when page reloads from net,but the problem with that is..the scrolling happens even thou submit button is not clicked. So can someone gimme the code for scrolling down to a div only when the submit button is clicked and after when the page is reloaded.



the code is something like this



<form name=searchdonor action= id=form method=POST align=center enctype=application/x-www-form-urlencoded>  
--- ------ ------
--- ------ ------
--- ------ ------

</form>
<input type=submit name=submit id=submit value=Submit onclick=return(searchval()); >
//the div i want to scroll down is below one
<div class=col-sm-8 id=what>
</div>


here's the complete code in html and php-mysqli



<div class=col-sm-4>
<h1 class=register-title>Search a Donor</h1>
<div id=wrapper>
<div id=chatbox>
<form name=searchdonor action= id=form method=POST align=center enctype=application/x-www-form-urlencoded>
<table id=results>
<tr><td><h4>Country:</h4></td><td>&nbsp&nbsp&nbsp&nbsp&nbsp
<select id=slct1 name=country onchange=populate1(this.id,'slct2')>
<option value=>--Select--</option>
<option value=India>India</option>
</select></td></tr>
<tr><td><h4>State:</h4></td><td>&nbsp&nbsp&nbsp&nbsp&nbsp
<select id=slct2 name=state onchange=populate2(this.id,'slct3')>
<option value=>--Select--</option>
</select></td></tr>
<tr><td><h4>District:</h4></td><td>&nbsp&nbsp&nbsp&nbsp&nbsp
<select id=slct3 name=district onchange=populate3(this.id,'slct4')>
<option value=>--Select--</option>
</select></td></tr>
<tr><td><h4>City:</h4></td><td>&nbsp&nbsp&nbsp&nbsp&nbsp
<select id=slct4 name=city>
<option value=>--Select--</option>
</select></td></tr>
<tr><td><h4>Blood group:</h4></td><td>&nbsp&nbsp&nbsp&nbsp&nbsp
<select name=bloodgroup>
<option value=>--Select--</option>
<option value=A+>A+</option>
<option value=A->A-</option>
<option value=B+>B+</option>
<option value=B->B-</option>
<option value=O+>O+</option>
<option value=O->O-</option>
<option value=AB+>AB+</option>
<option value=AB->AB-</option>
</select></td></tr>

</form>
</table><br />

<input type=submit name=submit id=submit value=Submit onclick=return(searchval());>
</div>


</div>
</div>

<div class=col-sm-2>
</div>
</div>
<div class=dropdownwrap>
<?php
if(isset($_POST['submit'])){

$country=$_POST['country'];
$state=$_POST['state'];
$district=$_POST['district'];
$city=$_POST['city'];
$bloodgroup=$_POST['bloodgroup'];
?>
<div class=row><br />
<div class=col-sm-2>
</div>
<div class=col-sm-8 id=what>
<a class=what></a>
<?php echo <h4 align='center'> Donors in <b>.$city.</b> for <b>.$bloodgroup.</b> are</h4>;?>
</div>
<div class=col-sm-2>
</div>
</div>
<div class=row>
<div class=col-sm-2>
</div>
<div class=col-sm-8>
<div class=table-responsive>

<table id=tablepaging class=table align=center>
<thead><hr />
<tr>
<th><b>Full Name</b></th>
<th><b>Contact Number</b></th>
</tr>
</thead>
<tbody>
<?php


$connect = mysqli_connect('localhost', 'root', '', 'blood'); if (mysqli_connect_errno()) echo Failed to connect to MySQL: . mysqli_connect_error();
$query=SELECT * FROM users WHERE country='$country' && state='$state' && district='$district' && city='$city' && bloodgroup='$bloodgroup' && activity='available';
$result=mysqli_query($connect,$query) or die('Error, query failed');
mysqli_close($connect);
if (mysqli_num_rows($result) == 0) {
echo<h3 align=center>Sorry, No Donors Found</h3>;
}
elseif($result)
{
while ($row = mysqli_fetch_array($result)){

echo<tr>;
echo<td>.$row[firstname].&nbsp.$row[lastname].</td>;
echo<td>.$row[phonenumber].<br />.$row[secondnumber].</td>;
}
echo</tr>;

}}
?>
</tbody>
</table>
<div id=pageNavPosition align=center></div>
</div>
</div>



<div class=col-sm-2>
</div>


</div>

</div>

</div>

More From » php

 Answers
64

You can use an anchor in the URL.



Or, if you want a smooth animation, just insert the JS code in the if(isset($_POST['submit'])) condition. This way, the scroll will happen only when the submit button is clicked and the page reloads.



I also suggest you use the $(function() { /**/ }); jQuery syntax so the scroll happens only when the DOM has been loaded.



<?php

if(isset($_POST['submit']))
{

//[...]
//sql query and display
//[...]

?>
<script>
$(function() {
$('html, body').animate({
scrollTop: $(#myDiv).offset().top
}, 2000);
});
</script>
<?php

}

?>


Code for scrolling to a specific div found here:
Smooth scroll to div id jQuery


[#64874] Tuesday, September 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevenirvina

Total Points: 315
Total Questions: 112
Total Answers: 84

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
;