drop down selection page reload function for multiple variables - javascript

I am working on a piece of code that will display the content of a mysql query in a template, since the query could feasibly return hundreds of results Ive included a page limiter and navigation tool. Unfortunately I am having a problem passing the page number after I select the number of results to show per page. The page nav is separate and working fine except it cant see the page number once this function has run.
<script>
function refreshpage(results_per_page){
window.location="classifieds.php?&results_per_page="+results_per_page
}
</script>
<?php
$results_per_page = $_GET['results_per_page'];
?>
<select onchange="refreshpage(this.value);">
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
The above code works fine and automatically reloads the page, but when ever i try to introduce a $page variable the pageRefresh function fails to work. Is this the right way to go about what I am after or is there another way that will let me include another variable. All it needs to be is;
$page;
taken from the url
$page = $_GET['page'];
Ive tried sending it with the onchange function, calling it from within the refreshPage Function, even using an intermediate function to assemble an extension with all the relevant data and nothing seems to give the result.
Thanks
EDIT:
Thanks to sasse, I got enough Info to correct the code I believe. This seems to now work so if anyone else has had this issue, this seems to be a good work around. Thanks! (never occurred to me to use val!)
<script>
function refreshpage(results_per_page){
var page = <?php echo $_GET['page'] ?>;
window.location="classifieds.php?page=" + page + "&results_per_page="+results_per_page
}
</script>
<?php
$results_per_page = $_GET['results_per_page'];
?>
<select onchange="refreshpage(this.value);">
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>

Since you're calling the "refreshPage" with onchange for the "select"-element and sending "this.value" you will send the selected value to the method.
Therefore you will send 1, 2, 3, 4 or 5 to the refreshPage method as parameter "results_per_page".
I would think you don't want one result per page, but I might be wrong.
When are you setting either "results_per_page" or "page"? My thought was that the "select"-element was for the "page" attribute and therefore the method "refreshPage" should accept "page" as parameter.
I'll write some code to demonstrate my intention.
<script>
function refreshpage(page){
var results_per_page = <?php echo $_GET['results_per_page'] ?>;
window.location="classifieds.php?page=" + page + "&results_per_page="+results_per_page
}
</script>
<select onchange="refreshpage(this.value);">
<!-- If this select is for "page" then I don't understand this next line -->
<option selected value="<?php echo $results_per_page?>"><?php echo $results_per_page?></option>
<option value="1">1</option>
<option value="2">2</option>
...
</select>
If the case is that I didn't understand correctly then tell me what I thought wrong and I'll update my answer. And also, update your question with what you've tried so far as long as the "page" goes, when is it set? Do you have a "next" and a "previous" button?

Related

Passing form Value into PHP include inside HTML DOM innerHTML

I am trying to pass a value from a <select> dropdown into a HTML DOM innerHTML JS so it is written into a div on the page. However I want the value to be passed into a PHP include so it changes the included php content being passed into the div on change of the dropdown.
I have gotten it to work passing the value into the innerHTML w/o wrapping it in a php include. So it will write into the div as 'purple.php' instead of the file itself, but when I try and wrap it in an include it breaks.
This is what I have gotten to so far.
Thanks for input, I have searched to no avail.
<script>
function gift(value)
{
document.getElementById("gift_post").innerHTML=('<?php echo include (value);?>');
}
</script>
<form action="" method="post">
<select name="slct" id="name" onchange="gift(this.value)">
<option value="" selected="selected"> Select </option>
<option value="purple.php"> purple </option>
<option value="green.php"> green </option>
<option value="blue.php"> blue </option>
</select>
</form>
<div id="gift_post"></div>
Thank you #O.Rares with your help I was able to figure it out. I stripped it down from what you had suggested since the .php is in the value so it doesn't need to be passed to the var. So that could be stripped. Then if i were to use the .load() function instead of php_include I dont need to worry injecting the .php into the innerHTML function because it can be passed directly with the form values. So I ended up with this and it is working perfectly. (note: '$' was changed to 'jQuery' due to further Wordpress conflict.)
<script>
function gift(value){
jQuery("#gift_post").load(value);
}
</script>
<form action="" method="post">
<select name="slct" id="name" onchange="gift(this.value)">
<option value="" selected="selected"> Select </option>
<option value="purple.php"> purple </option>
<option value="green.php"> green </option>
<option value="blue.php"> blue </option>
</select>
</form>
<div id="gift_post"></div>
function gift(value)
{
document.getElementById("gift_post").innerHTML=('<?php echo include (' + value + ' ); ?>');
}

Return a variable from jquery and use in jsp

I have a jsp file that contains a jquery function that listens to the onchange event of a dropdown element.
..
<select id="pick">
<option value="">Select one...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
....
<script type="text/javascript">
$('#pick').on('change', function() {
var isClicked=false;
if($(this).val()==2){
isClicked=true;
}
});
}
</script>
Now, this script is in a jsp code. The problem is I want to use the boolean isClicked when the function is out of scope. I learnt I have to set a variable to read off the state of the boolean in the jquery dom . I don't know how to do this and also, I don't know how to finally use the variable set in the dom in my jsp code. Any help will be appreciated
Well, you can't just seamlessly use client-side and server-side code interchangeably. You would have to send the stored values over the network via an AJAX request.

PHP Simple HTML DOM and Dropdown Element Selected Option

I'm generating dropdown inputs with php simple html dom. I have some values from database and according them i will add 'selected' value into my select->option dom elements.
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
this is the default one. i would like to add value like this, check on option 2 :
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2" selected>Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
and while doing that i want to use my plugin.
<?php
$html = new simple_html_dom();
$html->load('
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
');
echo $html;
?>
Everything works nicely until here. Yeah now i need to insert selected into my second option. I don't know how to do this with PHP Simple HTML DOM, or i'm missing something in the documentation : http://simplehtmldom.sourceforge.net/manual.htm#section_access
What i have tried so far and got many errors in my php section :
<?php
$html = new simple_html_dom();
$html->load('
<select name="myDropDown" id="someID">
<option value="someValue1">Value1Value</option>
<option value="someValue2">Value2Value</option>
<option value="someValue3">Value3Value</option>
</select>
');
//here's where i'm trying to reach the child nodes :
$ret = $html->getElementById('someID');
$ret->children(2)->GOTTADOSOMETHINGHEREANDIREALLYDUNNO;
echo $html;
?>
By the way, if you offer another easy way to do this, i'd appreciate.
Thank you in advance !
With minimal research you can figure this out for yourself.
$ret->children(2)->selected = true;
Why you use simple_html_dom and not simply a template file (with PHP)?
<select name="test" <?php if($selected){echo 'selected'}?> />
Or even better Smarty.
<select name="test" {if $selected}selected{/if} />
Simple_html_dom is a class for parsing complex html documents like exchanging image urls or something like that. I cant' see any reason why you need to use this class.
for getting Dropdown Element Selected Option with Simple HTML DOM
just try this simple and easy method
$element = $html->find('#selectIDGoesHere',0)->find('option');
foreach($element as $elemen) {
echo "Display text:".($elemen->plaintext)."<br>";
echo "value:".($elemen->value)."<br>";
}

Bootstrap Multiselect: On submit getting length of selected options list instead of option values

This is my first time using Bootstrap Muliselect; it's been a completely elegant experience until this:
When my form is submitted, what comes through the POST request is the number of selected options from each select.multiselect element, not their values, ie:
If I create:
<select name='foo'>
with 3 options
<option value='b' selected='selected'>B</option>
<option value='a' selected='selected'>A</option>
<option value='r' selected='selected'>R</option>
subsequently typing print_r($_POST) produces:
Array
(
[foo] => 3
)
I feel like this must be a facepalm sort of moment, but I've been at it for about two hours and though I can write ways to achieving this end, I can only assume Bootstrap is supposed to have such functionality by default. Anyone able to point me in the right direction, here?
Notes:
For sure dependencies are installed & jQuery is loaded properly
I'm using CakePHP & Foundation 4, but I don't think there are any conflicts/collisions
I'm submitting the form via $("#MahFormNaym").submit(); which I think doesn't matter but I am a JavaScript nub
The PHP/HTML:
<select id="<?php echo $model;?>-filter" class="multiselect" multiple="multiple">
<?php foreach($elements as $id => $element):?>
<option value="<?php echo $id;?>"> <?php echo $element;?> </option>
<?php endforeach;?>
</select>
The JavaScript:
$(".multiselect").multiselect({
buttonWidth:"600px",
includeSelectAllOption: true,
enableFiltering:true
});
You haven't assigned your option a corresponding value to reflect [foo] => 3. So, in order for this object to work like what you wanted, you need to assign the value with number like this:
<option value='0' selected='selected'>B</option>
<option value='1' selected='selected'>A</option>
<option value='2' selected='selected'>R</option>
Once node finds the specific number that matches to value, it'll then grab the content of the selected.

Dropdown auto select for page to page

How can I auto select from drop down.
Like where customer/reserve.php then click the dropdown and goes to customer/reserve_time&date.php
I'm comfortable using PHP if there's any tutorials can you link it to me
I search in Google but not a single one comes out.
I think you wants to redirect to another page whenever user changes dropdown value. So, you can do a javascript function call on dropdown change event.
<select name="dropdown" onchange="redirectPage(this.value);">
<option value="url1">Option A</option>
<option value="url2">Option B</option>
</select>
Your javascript function starts here:
<script type="text/javascript">
function redirectPage(value)
{
window.location.href = value;
}
</script>
Note: url1 and url2 are the link for the pages. Try This...
You need get in the second page the value selected for the user at the first page.
If you are using GET
$value = $_GET['param'];
If you are using POST
$value = $_POST['param'];
-
<select>
<option value="foo" <?php if($value=="foo") echo "selected"; ?>>bar</option>
<option value="foo1" <?php if($value=="foo1") echo "selected"; ?>>bar2</option>
</select>
UPDATE: Change page and pass parameter when select option.
<select onchange="window.location.href='other.php?value=' + this.value">
<option value="">-- Select one --</option>
<option value="men">Men</option>
<option value="woman">Woman</option>
</select>

Categories

Resources