How to Add a Selected Option Value to Search Query - php

So you can check out the search on my site at http://www.CyberBytesInc.com it's working just fine at the moment. What I am trying to do though is I have a search parameter dropdown next to the search, one "Search Entire Site" and the other "Search Blog". I am using Google Custom Search so to search just my blog all I need to have before the search term is "site:cyberbytesinc.com/blog/" aka, the value of the dropdown.
Here's the HTML:
<div id="search" class="hidden">
<form method="get" action="/search" class="row no-p-mobile">
<select id="search-parameter" name="parameter">
<option value="">Search Entire Site</option>
<option value="site:cyberbytesinc.com/blog/">Search Blog</option>
</select>
<input id="search-field" name="q" type="search" placeholder="Enter to Search"/>
<span class="visible-desktop"><input class="red-btn" type="submit" value="Search ›"></span>
<!-- x-webkit-speech -->
</form>
</div>
Pretty much if they leave it on "Search Entire Site" which is the default, then I want it to do just as it already does, so I have the value set to nothing. But if they select "Search Blog" then I want them to type in their search query next to it, and upon hitting enter, it would just take the value of the option they selected and throw it into the URL right before their search term, so it would go from:
.com/search?q=example
to
.com/search?q=site:cyberbytesinc.com/blog/%20example
I know this has to be very straightforward, I am just pretty new with all of this and have spent a bit trying to figure it out on my own and I'm at the point where I could just use some quick help!
Thanks so much guys/gals (:
EDIT (I'm VERY Close now, Just Need A Little Help):
I have tried numerous things, for instance, naming both the option and the textfield the same name, e.g. "q" and I have managed the url to become this when searching just the blog:
.com/search?q=site%3Acyberbytesinc.com%2Fblog%2F+&q=example
So it's actually pulling in the proper things into the URL when selecting just the blog in the dropdown. The problem is, Google Custom Search is again just recognizing the last
"q=example"
and ignores the first
"q=site%3Acyberbytesinc.com%2Fblog%2F"
even though there is a + inbetween the two.
If I could somehow get the term to carry over and not have the second "q=" then it would work, because it works perfectly if you just go in and remove the second "q=" in the URL structure it makes.

You're going to need to change this:
<div id="search" class="hidden-desktop">
<ul class="row no-p-mobile p-top-10 p-bottom-10">
<li>
<select id="search-parameter" name="parameter">
<option value="">Search Entire Site</option>
<option value="site:www.cyberbytesinc.com/blog/%20">Search Blog</option>
</select>
</li>
<li><form method="get" action="/search"><input id="search-field" type="search" name="q"/></form></li>
</ul>
</div>
to this
<div id="search" class="hidden-desktop">
<ul class="row no-p-mobile p-top-10 p-bottom-10">
<li>
</li>
<li><form method="get" action="/search"><input id="search-field" type="search" name="q"/>
<select id="search-parameter" name="parameter">
<option value="">Search Entire Site</option>
<option value="site:www.cyberbytesinc.com/blog/%20">Search Blog</option>
</select>
</form></li>
</ul>
</div>
You need to make sure the select element is inside the form element so that it's associated appropriately, as that form element will define how and what is sent to the server. That'll automagically put the value of the select in the URL query string and the rest is all up to you.

Related

PHP Goutte. Change Select Field and capture Ajax Changes - Scraping BigCommerce

I am currently scraping a website running BigCommerce.
The variations have dropdown boxes, when the end user changes one of the select boxs the product information changes, the URL doesn't change.
When the select box is changed it POSTS the product ID and Variant ID, the page is then updated.
How can I mimic this select box change? I have tried to replicate the POST but I get a security token error as I suspect it's blocking cross domain scripting.
The page URL doesn't update so I can't even loop through pages, the form code is here
<div class="productView-options-wrap" data-product-option-change="" style="">
<div class="productView-options-inner">
<div class="form-field" data-product-attribute="set-select">
<label class="form-label form-label--alternate form-label--inlineSmall" for="attribute_select_176">
Color: <small>Required</small>
</label>
<select class="form-select form-select--small" name="attribute[176]" id="attribute_select_176" required="">
<option value="">View Options</option>
<option data-product-attribute-value="1126" value="1126">1 Standard (Most Popular)</option>
<option data-product-attribute-value="1127" value="1127">1 Standard</option>
</select>
</div>
</div>
</div>
<div class="form-field form-field--increments">
<label class="form-label form-label--alternate" for="qty[]">
Quantity:
</label>
<div class="form-increment" data-quantity-change="">
<input class="form-input form-input--incrementTotal" id="qty[]" name="qty[]" type="tel" value="1" data-quantity-min="1" data-quantity-max="0" min="1" pattern="[0-9]*" aria-live="polite">
</div>
</div>
<div class="form-action">
<input id="form-action-addToCart" data-wait-message="Adding to cart…" class="button button--primary" type="submit" value="Add to Cart">
</div>
The ajax being called is
var formData = {
"action": "add",
"attribute[176]": "1130",
"product_id": "931",
"qty[]": "1"
};
$.post('https://example.com/remote/v1/product-attributes/931', formData)
.done (showResult)
.fail(showError);
Thanks
There are 2 ways you can go about doing this.
You can use the V3 API, fetch all of the products, including their variants. You can use that data to hit the Product/Variant Options Endpoint, where you'll be able to read all the options metadata. You will be able to manually re-construct the HTML that way - although it's really not ideal.
An alternative would be to determine the SKU of each product variant, and append that as a query parameter to the product page URL.
If you include the SKU as a query parameter to the product page, it will render out the pre-selected option configurations.
I'm a bit confused as to what you're trying to accomplish with the scraped data? If you're going to be performing actions on it, you'll likely run into further CORS issues.
I ended up re-writing this using use Symfony DomCrawler and Guzzle
I was able to save the security token and submit with the ajax call and process the data that way. 700+ products in around 10 minutes.

How do I Apply a working Dropdown instead of a Text field in php

I am working on customizing a plugin, however my knowledge is currently lacking on the php side.
currently i have an Ad plugin in which you can sell ads per day, however my predicament is that i want to sell the ads only in packs of 30,60 & 90 days.
In this plugin upon typing a number of days this plugin calculates the amount and gives a result right under without anything pressed, so it is actively reading an input; however the only problem with this plugin is that it allows anyone to type a random number in there.
now the only real solution that i could think of was to make a drop-down with pre-existing values 30, 60, 90
My attempts thus far have been futile as i keep applying HTML <Select> (yes i had changed the wording from <input to <select and then added <option value="value">value wording</option>)techniques and it seems that anytime that i attempt to put a number for the value as one of the choices i get nothing but an empty drop-down, this then made sense to me since this is a PHP form and not an HTML form.
I was able to find where to code the changes, as i was able to find where the field is pulling from; here is the code:
(How should i proceed to make the changes that i want happen?)
<div class="control-group" id="total_days" style="<?php echo $date_dis;?>">
<label class="ad-price-lable control-label" width="40%"><div id="total_days_label"><?php JText::_("TOTAL_DAYS_FOR_RENEWAL");?>:</div> </label>
<div class="controls">
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
<input type="hidden" name="ad_totaldays" id="ad_totaldays" value="<?php echo $ad_totaldays;?>" />
</div>
</div>
now i am also assuming that out of this area, the main thing to be changed would be:
<input type="text" maxlength="5" name="totaldays" class="ad-pricing" id="totaldays" value="<?php echo $ad_totaldays ;?>" onchange="caltotal()" >
of course, i could be wrong, but i really would appreciate help with this!
thank you very much!
Here is how I've always done dropdowns
<select form="examplef">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<form id="examplef">
<input type="submit">
</form>
The php you're looking at is setting a value field for the inputs. This just means that something will be in the box already when you load the page.
Also keep in mind that just because you have predefined options doesn't mean you wont get unexpected input; Changing the options is as easy as right clicking
PHP form vs HTML form
They are the same. The html <form> tag is a way to communicate with php. This tag will (most likely) have an attribute called "action" and "method". The action attribute specifies what to do with the collected information. You can think of it almost as an <a href> that includes data.
The method attribute is a little more complicated. It tells your browser how to send the data entered into the form. The two options are "GET" and "POST". the GET method is data put in the url. I'm sure you've seen youtube links with the ?id=. This is just a GET variable included in the URL. The POST method is for secure data, or data that shouldnt be in the URL. If you would like to read more, W3Schools has a great document here or from Php here and here

Change form action based on selected item

I'm new to html, but I need to make this block of code functional:
<form method="get" action="http://cat.opal-libraries.org/search~S9/X">
<p>
<select name="searchtype">
<option value="X" selected="selected">Keyword</option>
<option value="t">Title</option>
<option value="a">Author</option>
<option value="d">Subject</option>
</select>
<input class="textfield" name=" " style="width: 200px;" type="text">
<input class="button" value="Search" type="submit">
I want to be able to select say a Title, and enter in a title, and hit search, and have it work like i entered it in this page
http://cat.opal-libraries.org/search/t
I would appreciate any suggestions on doing this!
Sorry for the non-answer, but as I mentioned, the answer to your question would be too long to be suited to Stack Overflow.
I suggest you get your hands dirty by following a PHP tutorial like this one.
As a side note, your HTML is invalid. You opened <form> and <p> tags on lines 1 and 2, and didn't close them. Semantically, it's a little strange placing those form elements in a paragraph. That's not really what a paragraph does.
Question is very broad as there are many ways to approach it. Here is a simple edit of your code that makes it work if you want to search by title:
<form method="get" action="http://cat.opal-libraries.org/search/t">
<input class="textfield" name="SEARCH" style="width: 200px;" type="text">
<input class="button" value="Search" type="submit">
</form>
What I did was fix your URL and give a name to the input. You probably want to read up on how <form method="get" works and then you can probably figure out how to search by different fields.
Updated to include full code. It'd be good if you read up on these items to find out how/why it works. Basically when they choose a new search type, you need to change the action of the form (that is the piece you were missing). I also added the "searchscope" field and set it to 28 since their website was doing that. I don't know what that does.
This assumes you are referencing jQuery. Related documentation jQuery attr() and jQuery change().
<form id="searchForm" method="get" action="http://cat.opal-libraries.org/search/X">
<select id="searchType">
<option value="X" selected="selected">Keyword</option>
<option value="t">Title</option>
<option value="a">Author</option>
<option value="d">Subject</option>
</select>
<input class="textfield" name="SEARCH" style="width: 200px;" type="text">
<input name="searchscope" type="hidden" value="28">
<input class="button" value="Search" type="submit">
</form>
<script>
$("#searchType").change(function () {
var newPath ="http://cat.opal-libraries.org/search/" + $("#searchType").val();
$("#searchForm").attr("action", newPath);
});
</script>
It looks like the search engine you're referencing uses a separate endpoint for each "searchtype," which actually makes this relatively easy to do with Javascript and a couple changes to your form.
Change the name attribute on the <select> to an id instead, so that it isn't actually sent to the search engine.
Change the name attribute of the text field to "SEARCH" so that it will be sent and recognized as the search string.
Add an on-change listener to the sel which modifies the action attribute of the form as appropriate. If you're using JQuery, that might look something like this:
var base = "http://cat.opal-libraries.org/search/";
$("#searchtype").change(function() {
$(that).closest("form").attr("action", base + $(that).val());
});

How to Get the inner text of a span in PHP

Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span's text does change. I am 15, so give me a break! I need some help! This is were the values are picket.
<div>
<select id="test_select" style="display:none;">
<option value="1">Graphic Designer</option>
<option value="2">Animator</option>
<option value="7">Announcer</option>
<option value="11">Web Developer</option>
<option value="12">Judge</option>
</select>
</div>
That is what needs to be gotten:
<div class="custom-select">
<span>Select Option</span>
<ul>
</ul>
</div>
Ok, you're going to need more than just PHP to get this done; you'll need JavaScript as well.
Let's start with your HTML. I'm assuming your rendered output looks like the following and I won't question why you're doing it this way.
<div id="dropdown">
<span>Option One</span>
<span>Option Two</span>
<span>Option Three</span>
</div>
So there's my guess at your HTML.
To post a value back to PHP, you're also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.
<input type="hidden" name="dropdown-selection" id="dropdown-selection" />
So that's our markup done. Next you'll need to grab the selected option from your div and stick it into the hidden field. I'm assuming you've coded something to render your div to look and behave exactly like a dropdown (ok, I'll bite. Why ARE you doing it this way?).
This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that's not true. Well, maybe it's a little bit true)
<script type="text/javascript">
$(function () {
$('#dropdown-selection').val($('#dropdown span:visible').text());
});
</script>
Now, as long as you've ensured that the hidden field is contained within a form that posts back to your target PHP page, you'll have the value of the span tag available to you.
I've made quite a few assumptions about how you've gone about setting up your page here. Correct me on any parts that don't tie into reality.

how to create a search form in php and mysql with pagination?

I am trying to create a search form with php and mysql and do a pagination
so i have a form that looks like this:
<form action="search_results.php" method="post">
<select name="cat" id="cat">
<option>Select Category</option>
<?php
foreach($categories as $category) {
echo "<option value=\"{$category -> name}\">{$category -> name}</option>";
}
?>
</select>
<select name="sub-cat" id="sub-cat">
<option>Select Sub Category</option>
</select>
<label>Price Range</label>
<input type="text" name="from" placeholder="From" />
<input type="text" name="to" placeholder="To" />
<input type="submit" value="" name="submit" />
</form>
my problem is that i get the first page in the search_result.php
and when i try to go to the second page the $_POST['submit'] wouldn't be set because it didn't get any submit
anybody has an idea how to solve this issue
Thanks in advance.
You should probably use GET in this case for the form and for the subsequent pagination links for the very reason that you specified.
POST is better used as a one off action where GET is a request for resources which can be filtered and hacked.
search_results.php?cat=1&sub-cat=2&page=2
search_results.php?cat=1&sub-cat=2&page=3
search_results.php?cat=1&sub-cat=2&page=4
search_results.php?cat=1&sub-cat=2&page=5
The easiest way would be to generate "next" and "previous" URLs that have the search information in the query string, and then have the receiving page read them from $_GET instead of $_POST. The $_GET array works exactly like the $_POST array, just for variables that were passed in the URL.
Another option would be to create a form where the search information is present in hidden fields, and use javascript to cause the "next" and "previous" links to change the value for the page to get, and then submit the form. This would allow you to use the exact same POST-based code you're using now, though it prevents users from bookmarking search pages and may have other unintended effects.

Categories