I have a search bar on my page, however it is about half way down, and when you submit a search query, the page scrolls back to the top.
Is there a way to get the page to stay where the user was when they search? My search is on the same page, and all of the content also stays on the page.
This is my current search code:
$search=mysql_real_escape_string($_POST['search']);
//- Queries the table to get content from rows -
$QuerySelect = "SELECT thumbnail,image,title,category,description
FROM homepage
WHERE category LIKE '%$search%' ";
//----------------- QUERY the TABLE, store THE CONTENT IN A PHP VARIABLE -----------------
$result = mysql_query($QuerySelect);
HTML :
<div class="searchcontainer">
<form method="post" action="index.php">
<input type="text" class="input" name="search" size="40" placeholder="Search by Category...">
<input type="submit" class="button" name="Submit" value="Search" id="Submit" >
</form>
</div>
<?php
$numrows = mysql_num_rows ($result );
if($numrows == 0){
Javascript & JQuery code is fine.
If you are posting to the same page you are on, you can make your action be index.php#my-form and give your form an ID of my-form. Of course change my-form to whatever you want.
Related
I am creating a database wherein users can select a title (search.php) and then the specific page for that title will be shown with its corresponding details (indiv.php).
They will be able to select a title from search.php and here is the code for the title link:
<a href="indiv.php?titleID=' . $row['titleID'] . '">
Then the indiv.php will be able to show the details for that title based on the titleID they have selected from search.php. Here is the code for indiv.php:
<?php
include "databaseconnect.php";
$titleID = $_GET["titleID"];
$sql = ("SELECT titleID, authorsID, yearID,
FROM table
WHERE titleID = '$titleID'");
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
$mysqli->close();
?>
In this indiv.php, I also added a comment section form.
<form action="indiv.php" method="POST">
<input type="text" name="nameID" placeholder="Enter your name" required>
<textarea name="commentID" placeholder="Write your comment here" required></textarea>
<input type="submit" name="submit" value="Post Comment">
</form>
The problem is, every time I try to submit a comment, the page refreshes and does not show its corresponding details (just shows an error - see below) because $titleID = $_GET["titleID"]; cannot fetch the titleID anymore from search.php but tries to find it in indiv.php because of my <form action="indiv.php" method="POST"> in the comment section. Is there a way to prevent this from happening? I want to still be able to show the details based on titleID even after posting a comment.
The error is: Notice: Trying to access array offset on value of type null in...
Thanks in advance! I am not familiar with PHP so I hope you can help me out.
How to I get the input id of selected button, and post it from next php page? I have this code inside my form it's dynamically populate from my database.
<?php
include('connection.php');
$query = "Select * from tblproduct where categoryID = 1 and statusProd = 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id=$row["ID"];
$product=$row["product"];
$image=$row["images"];
echo '
<div class="col-sm-4 divProduct" style="outline: none;background-color: transparent;border:none;height:320px;width:300px">
<div class="content">
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
<div class="btnBuyBuy text-center">
<img src="'.$image.'" class="imgProd" style="width:200px;height:170px;">
<br><br>
<label class="nameProd">'.$product.'</label>
<br><br>
<div class="divBuy2">
<label class="lblBuy">BUY NOW</label>
</div>
</div>
</div>
</div>';
}
}
And here's my code to the next page where the id displaying.
<?php
if (isset($_POST['btnGame'])) {
$id = $_POST['prodID'];
echo "$id";
}
else{
echo "failed";
}
And the output is the last id from my tblproduct which is 13. How can I get the id of selected button?
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
You aren't trying to get the "id" (you mean value) of the button, you are trying to get the value of the hidden input next to it.
The problem with this is that proximity to the clicked submit button means nothing. All hidden inputs will be successful controls. All will be submitted to the server. Since they all share the same name, only one of them will show up in $_POST. (If you renamed them so the name ended in [] then they would all should up as an array and you still couldn't tell which was selected).
Don't use a hidden input for this.
If you care about the button that is used, then make use of the submit button.
Only the submit button used to submit the form will be successful, so its name and value can be used to tell which one was clicked.
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="$id">
… and then look at $_POST['btnGame'] instead of $_POST['prodID'].
Given that you have value="" I'm guessing that you don't want any text displayed on the image and that you are using CSS background image to display something in it.
Obviously, the above won't be compatible with this, so use a <button> element instead.
That allows you to have a different label and value.
It also allows you to put elements inside the label, so you can use a content image with an alt attribute instead of a background image and score a big accessibility win.
<button name="btnGame" id="btnGame" class="btnSubmit" value="$id">
<img src="/path/to/icon.png" alt="Select product number $id">
</button>
At the bottom of this code you'll see an 'Accept Offer' button, when I click on that another piece of code gets executed as you can see on the bottom of this post.
For example this project has 3 bidders, so 3 times bidder_id and writer_bid so I use 'foreach' and load it in divs, works fine, but now I need to store those variables in a database, which technically works but it doesn't store the bids from the row I pull them from, it just takes the data from the last row, that is if I place the code at the bottom of this thread in my header.
However when I put it inside the loop it executes three times, I saw that when I got an error message that I had to close 3 times cause there are 3 rows in the database table that I pull the data from.
How can I prevent this, and either have it load once when the code is inside the foreach loop, or have it pull the correct writer_bid and bidder_id to store.
<div class="WB-Bottom-block lefts">
<?php $getBidders=" AND project_id=$project_id"; $bidders=getBidder($getBidders); foreach($bidders as $bidder) {
$bidder_id=$bidder['writer_id'];
$writer_bid=$bidder['writer_bid'];
?>
<div class="findwriters_boxes">
<div class="findwriters_right">
<div style="float:right;margin-top:6px;width:170px;">
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="bidder_id" id="bidder_id" value="<?php echo $bidder_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</div>
</div>
</div><?php } ?>
Below the code that needs to be executed and that results in issues, whether I place it inside the foreach loop, or inside the header instead.
As you can see I tried to store it in input fields so that it stays there so the header can pull it on refresh of the page / click of the button.
<?php if(isset($_POST['todo']) && $_POST['todo']=='submit_project') {
$balance=get_client_balance_info($current_user->ID);
$writer_bid=$_POST['writer_bid'];
$bidder_id=$_POST['bidder_id'];
if($balance >= $_POST['writer_bid']) {
global $wpdb;
$sql3="UPDATE `wp_project` SET `writer_id` = '".$bidder_id."' WHERE `id` =". $project_id;
$wpdb->query($sql3);
$sql4="UPDATE `wp_project` SET `price` = '".$writer_bid."' WHERE `id` =". $project_id;
$wpdb->query($sql4);
$sql5="UPDATE `wp_project` SET `status` = '2' WHERE `id` =". $project_id;
$wpdb->query($sql5);
$success_msg="You accepted a bid, the money will be deducted from your account.";
}
else $fail_msg="Your balance is not sufficient.";
I think you should make a form for each div that you are adding right now you are putting the bidder_id in the different inputs but the same name.
So it will get the last inputs, maybe it's better to specify the inputs with the row id or to separate the forms or make the input names as array.
I hope this helps you.
I fixed it with the help of Diar Selimi like this:
<div style="float:right;margin-top:6px;width:170px;">
<form action="" name="frmeditor" method="post" id="frmeditor" >
<input type="hidden" name="todo" id="todo" value="submit_project" />
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="writer_id" id="writer_id" value="<?php echo $writer_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</form>
Before that my form and value="submit_project" tags were scattered all over the place!
I'm finding it very hard to return database results with php pdo pagination in wordpress. I have a form on another page that sends the search data to search4.php where I want to display matching rows and have previous|next links. I get no results, and If I echo $search, it just says 'search'
Here is the relevant code so far:
//html form on another page
<form method="POST" action="<?www.example.com/search4 ?>">
Search:
<input type="text" name="search"
<input type="submit" name="search" value="search" /></form>
//search4.php relevant code
if(isset($_REQUEST["search"]) && $_REQUEST["search"] != "")
{
$search = htmlspecialchars($_REQUEST["search"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.category LIKE
'%$search%' OR stories.genre = LIKE '%$search%'");
$pagination->config(3, 8);
$sql = "SELECT * FROM stories WHERE stories.category LIKE '%$search%' OR
stories.genre = LIKE '%$search%' ORDER BY SID ASC LIMIT $pagination-
>start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
...etc
You <input type=text> and your <input type=submit> has the same name... So button value is overriding your text value... that why is always "search".
change it to:
<input type="text" name="search_text"/>
<input type="submit" name="search_button" value="search" />
Now, on your search4.php you can access your search text using $_REQUEST["search_text"]
PD: You can remove the name attribute on the submit button too.
I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.