first of all, I'm sorry, I am new in this community also my English is not so good, I hope you guys will understand. The question I want to ask is
Currently I'm doing my directory website project, that display all the data in database in pagination because of the massive data in database. User need to select the the state and cluster, then the website will show the list of result. But all the list must be in pagination. I expected when click the page link, it will take the user to 2nd page and so on.
current website display
But the actual result is, when I click the 2nd page, it shows nothing. Like this: the website page after click 2nd page
I am still new in PHP code. I've tried everything I could. I would like to ask, is there something wrong in my coding?
here is some of my coding for "index.php"
enter code here
<form method="post">
<div class="input">
<select name="state" class="btn" required>
<option value="" class="placeholder" selected disabled>Carian Mengikut Negeri</option>
<option value="Johor">Johor</option>
<option value="Kedah">Kedah</option>
<option value="Kelantan">Kelantan</option>
<option value="Melaka">Melaka</option>
<option value="Negeri Sembilan">Negeri Sembilan</option>
<option value="Pahang">Pahang</option>
<option value="Penang">Penang</option>
<option value="Perak">Perak</option>
<option value="Perlis">Perlis</option>
<option value="Sabah">Sabah</option>
<option value="Sarawak">Sarawak</option>
<option value="Selangor">Selangor</option>
<option value="Terengganu">Terengganu</option>
<option value="Wilayah Persekutuan">Wilayah Persekutuan</option>
</select>
<select name="cluster" class="btn" required>
<option value="">Kluster Perniagaan</option>
<option value="Makanan dan Minuman">Makanan & Minuman</option>
<option value="Gaya Hidup">Gaya Hidup</option>
<option value="Automotif">Automotif</option>
<option value="Pembinaan">Pembinaan</option>
<option value="Perkhidmatan">Perkhidmatan</option>
</select>
<input type="submit" name="submit" value="Cari" >
</div>
</form>
<div class="output">
<?php
include "connect.php";
?>
</div>
And here is my "connect.php" that has a query (sorry, i cant show the full code at code sample, so i use snippet)
<?php
$conn = new mysqli('localhost','root','','bpuvirtu_Directory') or die("Connection Failed");
if(isset($_POST["submit"]))
{
$result_per_page = 10;
$str= $_POST["state"];
$cluster=$_POST["cluster"];
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster'";
$result=mysqli_query($conn,$sth);
$number_of_results=mysqli_num_rows($result);
$number_of_results;
$number_of_pages= ceil($number_of_results/$result_per_page);
if(!isset($_GET['page'])){
$page=1;
}
else{
$page=$_GET['page'];
}
$this_page_first_result = ($page-1)*$result_per_page;
$sth="SELECT * FROM TABLE3 WHERE state='$str' and service='$cluster' LIMIT " . $this_page_first_result . "," . $result_per_page;
$result=mysqli_query($conn,$sth);
?>
<table id="user">
<tr>
<th>Nama Syarikat</th>
<th><center>Kluster</center></th>
<th><center>Info</center></th>
</tr> <?php
while($row=mysqli_fetch_array($result)){
?>
<tr>
<form method="GET" action=detail.php>
<td><?php echo $row["name"]; ?></center> </td>
<td><center><?php echo $row["service"]; ?></center> </td>
<input type='hidden' name='id' value='<?php echo $row["id"]; ?>'/>
<td><center><input type="submit" name="detail" value="Info" ></center></td>
</form>
</tr>
<?php
}?></table><?php
for($page=1;$page<=$number_of_pages;$page++){
echo '' . $page . '';
}
}
?>
So, when I click page 2, I want it show the list from database at page 2.
You can try this:
//$page = grab page number from the query param here.
return redirect('currencies?page='.$page);
or:
$results->url($page)
Related
I want send the data I received from the select tag through the url as queries so I can use it and query the database.
I have tried but the values are not showing. I am getting something like: type=&action=
This is my code
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
<a class="site-btn fs-submit" href="search.php?<?php echo $query; ?>"> Advanced search</a>
I expected :
type=2 bed room&action=rent
Try the following code. Here, When you submit the form the whole form data will be passed to search.php and the form method is POST so you can access those data by simply calling $_POST['attribute value of name'].
HTML form:
<form class="filter-form" method="post" action="search.php" enctype="multipart/form-data">
<input type="text "class="d-block d-md-inline" placeholder="Enter State, City or Area" name="city">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<input type="submit" value="Advanced search">
</form>
create a PHP file (search.php):
<?php
//Do what you want here
if (isset($_POST['city'])) {
$city = $_POST['city'];
echo $city;
}
if (isset($_POST['types'])) {
$type = $_POST['types'];
echo $type;
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
echo $action;
}
?>
Replace the link with a button. Attach the query params in the link to the action of the form. Your code should look like below.
<form action="search.php?<?php echo $query; ?>">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<?php
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
?>
<button class="site-btn fs-submit" type="submit">Submit</button>
</form>
in my page when a user login into his acount there are two fields name and project so whenever he login his named filled in name field and his all projects are listed in dropdown in project field.my problem is tha when i edit this record it shows me the name and the project selected by him on submission but i want that on edit his all project will b listed there as on submission time.
Here is my project code for dropdown on submission time:
<tr>
<td>Select Project</td>
<td>
<select id="project" name="project">
<option value="">-- select --</option>
<?php while($row = mysql_fetch_array($result))
{
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)){ ?>
<option value="<?php echo htmlspecialchars($row['projectname']);?>"><?php echo htmlspecialchars($row['projectname']); ?></option>
<?php } }?>
</select></td>
</tr>
and here is my code for project field on editing:
<tr>
<td>Select Project</td>
<td><select name="project" id="project" style="width:145px">
<option>Select</option>
<option selected="selected" value="<?php echo $fetuser1['project'];?>"><?php echo $fetuser1['project'];?></option>
</select></td>
</tr>
So when editing you must do things much the same as on "submission time".
<tr>
<td>Select Project</td>
<td>
<select name="project" id="project" style="width:145px">
<option>Select</option>
<?php
while($row = mysql_fetch_array($result)) {
$pm = $row['assignpm'];
$assignpm = explode(",",$pm);
if(in_array($_SESSION['id'], $assignpm)) {
?>
<option
<?php if($fetchuser1['project'] == $row['projectname']) echo "selected='selected'";?>
value="<?php echo htmlspecialchars($row['projectname']);?>"
>
<?php echo htmlspecialchars($row['projectname']); ?>
</option>
<?php } }?>
</select>
</td>
</tr>
depends...
what do your query look like?
one query? two querys? one on the userid, name and stuff and another on the projects...
or are all project in the same table as the fetched username?
and how are thy stored (blob, json, simple list)?
And last: mixing php inline with html is a very bad habbit ;)
say you have two querys
//query SELECT projectname FROM whatevertable WHERE userid = 'users-id'
$options = "<option value="">-- select --</option>".PHP_EOL;
while($row = mysql_fetch_array($result))
{
if(htmlspecialchars($row['projectname']) == $_SESSION['preselected']) //whatever
$options. = "<option selected="selected" value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
else
$options. = "<option value=".htmlspecialchars($row['projectname']).">".htmlspecialchars($row['projectname'])."</option>".PHP_EOL;
}
and after all the php code you can
echo "<td>Select Project</td>
<td>
<select id=/"project/" name=/"project/">
{$options}
</select>
</td>";
But without knowing any of your code but that little php-mixed-html it's hard to answer precisely
im new to php.i created a page in php containing chk boxes ,input fields and retrive the data from the database(mysql) and also use the javascript.
i h've a problem in this ,that is i want to display the output in the same page.
i created the code like this,i want the output in same page,but it shows error.
<?php
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","demosdef_review","review123");
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("demosdef_review", $con);
$state = $_POST["state"];
$country = $_POST['country'];
$contry = "USA";
if($country==1)
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='1' AND id='$state'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
else
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='$country'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
}
?>
enter code here
<div class=buy>
Dealer Enquiry
</div><br/>
<div class=buyfont>Authorized Retailers</div>
<div><img src="images/archivers.png"><br/>
<a href="http://www.archiversannex.com/Books-And-SoftwareSoftware-And-Accessoriesdefault.aspx?PageID=20&CategoryID=48"/>
<img src="images/logo_archivers_annex.png" ></a><br/><br/>
<br/><br/>
</div><br/>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
<p>
<label for="country" style="padding-right: 2px;">Country</label><select name="country" value="countryid"
id="countryid"
onchange="sub()" style="width:120px;">
<option value="1">USA</option>
<option value="2">CANADA</option>
<option value="3">UK</option>
<option value="4">AUSTRALIA</option>
<option value="5">ITALY</option>
<option value="6">GUATEMALA</option>
<option value="7">NEW ZEALAND</option></select></p>
<p>
<label for="state" style="padding-right: 14px;">State</label>
<select id="state" name="state" value="state" style="width:120px">
<option value="7">Alabama</option>
<option value="8">Alaska</option>
<option value="9">Arizona</option>
<option value="10">Arkansas</option>
<option value="11">California</option>
<option value="12">Colorado</option>
<option value="13">Connecticut</option>
<option value="43">Florida</option>
<option value="14">Georgia</option>
<option value="15">Idaho</option>
<option value="16">Illinois</option>
</select></p> <br/>
<input class="dealer-submit" type="submit" value="Submit" onClick="buy_func()"/>
</form>
<script type="text/javascript">
function sub()
{
var x=document.getElementById("countryid").selectedIndex;
var y=document.getElementById("countryid").options;
var z = document.forms[0].state;
if(y[x].index==0){
z.disabled = false;}
else if(y[x].index>0) {
z.disabled = true;}}
</script>
Your HTML markup has an error
<a href="http://www.hobbylobby.com/storelocator"/>
It should be
<a href="http://www.hobbylobby.com/storelocator">
Also try
<form method="POST" action="">
Keeping the action blank will post it to the same page itself
And replace your submit button with this
<input class="dealer-submit" type="submit" name="submit" value="Submit" onClick="buy_func()"/>
Check the value of the submit button, if its set isset($_GET('submit')) then display the results
does that solve your problem?
submit is the type of the element, 'Submit' is the value you are looking for:
<?php
if(isset($_POST['Submit']){
}
?>
Remove the backslash at the end of the line:
<a href="http://www.hobbylobby.com/storelocator"/>
^
I want to write a code that should let me select from a drop down list and onClick of a button load the data from a mysql database.However I cannot access the value selected in the drop down menu.I have tried to access them by $_POST['var_name'] but still can't do it.
I'm new to PHP.
Following is my code:
<?php
function load(){
$department = $_POST['dept'];
$employee = $_POST['emp'];
//echo "$department";
//echo "$employee";
$con = mysqli_connect("localhost", "root", "pwd", "payroll");
$rs = $con->query("select * from dept where deptno='$department'");
$row = $rs->fetch_row();
$n = $row[0];
$rs->free();
$con->close();
}
?>
<html>
<head>
<title>Payroll</title>
</head>
<body>
<h1 align="center">IIT Department</h1>
<form method="post">
<table align="center">
<tr>
<td>
Dept Number:
<select name="dept">
<option value="10" selected="selected">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</td>
<td>
<input type="button" value="ShowDeptEmp" name="btn1">
</td>
<td>
Job:
<select name="job">
<option value="President" selected="selected">President</option>
<option value="Manager">Manager</option>
<option value="Clerk">Clerk</option>
<option value="Salesman">Salesman</option>
<option value="Analyst">Analyst</option>
</select>
</td>
<td>
<input type="button" value="ShowJobEmp" name="btn1">
</td>
</tr>
</table>
</form>
<?php if(isset($_POST['dept']) && $_POST['dept'] != "") load(); ?>
</body>
</html>
change button to submit
<input type="submit" value="ShowDeptEmp" name="btn1">
and
<input type="submit" value="ShowJobEmp" name="btn2">
Use a prepared statement instead of echoing $department into your SQL. If someone posted '; DROP TABLE dept; they could run arbitrary SQL commands (See SQL Injection).
OR you can use mysql_real_escape_string() when escaping $department if you don't want to use a Prepared Statement.
i want to control the drop down box to control show or hide statement. I do like this but it seems it doesn't work, i have it working if im using radio button.
can help me with the code? which part am i wrong?
thank you.
$dbcnx = mysql_connect('localhost', 'root', '');
mysql_select_db('dbase');
if($_POST['gred'])$gred=$_POST['gred'];else $gred="";
<script language="JavaScript">
function funcHide(elemHide1,elemHide2,elemHide3)
{
document.getElementById(elemHide1).style.display = 'none';
document.getElementById(elemHide2).style.display = 'none';
document.getElementById(elemHide3).style.display = 'none';
document.getElementById(elemShow).style.visibility = 'visible';
}
function funcShow(elemShow1,elemShow2,elemShow3)
{
document.getElementById(elemShow1).style.display = 'block';
document.getElementById(elemShow2).style.display = 'block';
document.getElementById(elemShow3).style.display = 'block';
document.getElementById(elemShow1).style.visibility = 'visible';
document.getElementById(elemShow2).style.visibility = 'visible';
document.getElementById(elemShow3).style.visibility = 'visible';
}
</script>
<table>
<tr>
<td>Gred </td>
<td>:</td>
<td><select name="gred" id="gred">
<option value=""> </option>
<option value="A17" <?php if($gred=='A17')echo "selected";?> onClick="funcShow('box1', 'box2', 'box3');">A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
</td>
</tr>
<tr>
<td>TK</td>
<td>:</td>
<td>
<select name="tk" id="tk">
<option value=""> </option>
<option value="01" <?php if($tk=='01')echo "selected";?>>01</option>
<option value="02" <?php if($tk=='02')echo "selected";?>>02</option>
<option value="03" <?php if($tk=='03')echo "selected";?>>03</option>
<option value="04" <?php if($tk=='04')echo "selected";?>>04</option>
<option value="05" <?php if($tk=='05')echo "selected";?>>05</option>
<option value="06" <?php if($tk=='06')echo "selected";?>>06</option>
</select>
<?} ?>
</td>
</tr>
<tr>
<td colspan="2" valign="top">Status</td>
<td valign="top">:</td>
<td>
<?php
$qry = "SELECT * from dtable where userid='".$USER->id."'";
$sql = mysql_query($qry);
$row = mysql_num_rows($sql);
if($row==0)
{
?>
<input type=radio name="status" <?php if($status=='retake') {?>checked="checked"<?php } ?> value="retake" onClick="funcShow('box1', 'box2', 'box3');">Retake<br /></tr> <tr>
<td colspan='2'>
<div id="box1" style="display: none;">Last Date <br> Latest Date<br>
</div></td>
<td><div id="box2" style="display: none;">: <br> : <br></div></td>
<td>
<div id="box3" style="display: none;">
<?php $rsu[lastdate] ?> <br> <?php $rsu[latestdate] ?>
</div>
</td>
don't put the onClick attribute onto the option tag. You should use onChange on the select tag and then pass in this.value. Then based on that value you can decide which "box" to show/hide. Just a simple example:
<script type="text/javascript">
function showHide(selValue){
switch(selValue){
case "A17":
//show/hide whatever box you want.
alert(selValue);
break;
default:
//do nothing
alert('nothing');
}
}
</script>
<select name="gred" id="gred" onChange="showHide(this.value);">
<option value=""> </option>
<option value="A17"<?php if($gred=='A17')echo "selected";?>>A17</option>
<option value="A22" <?php if($gred=='A22')echo "selected";?>>A22</option>
<option value="A27" <?php if($gred=='A27')echo "selected";?>>A27</option>
</select>
for now, as you can tell, it just alerts some values. But it is easy to change.
Use onChange to trigger JS when you change something in the dropdown menu, instead of onClick. With selectedIndex you can determine what has been selected (the option 'show' or 'hide') and go from there.
Tip: get rid of all the junk code and limit it to just the elements that you need. Try to make it work in its most basic form, then build it out to what you're actually trying to do. That way it's a lot easier to track bugs and find out why it's not working.
Also, you might want to look into jQuery. You can do pretty much the same in plain old JavaScript, but jQuery makes things like this a lot easier.