I have problem with change images size in posts (after click on the buttons grid/list).
these are the buttons that should (after click) change $value to true or false (that scale images):
<div class="products_look">
<input type="button" class="view_list" onclick="" value="List" />
<input type="button" class="view_grid" onclick="" value="Grid" />
</div>
and this is the php functions that generated grid or list view in the posts:
<?php
if($value == true) {
echo image_products_grid();
}
else {
echo image_products_list();
}
?>
What I need to insert in onClick tags that after clicking on the button returned php $value (true or false)?
Why not use a parameter in the URL? Something like this:
<div class="products_look">
<input type="button" class="view_list" onclick="window.location = 'display.php?type=LIST'" value="List" />
<input type="button" class="view_grid" onclick="window.location = 'display.php?type=GRID'" value="Grid" />
</div>
And then on your PHP page:
<?php
if($_GET["type"] == "GRID") {
echo image_products_grid();
}
else {
echo image_products_list();
}
?>
Is this the type of solution that you are looking for? It's not terribly elegant, but your question is a little unclear so I hope this helps.
Related
So, I have a button in a form.
<form>
<button onclick="alert('<?php echo $value ?>')">Click me!</button>
</form>
$value = "1.png"
When I press it changes the url like this:
Initial:
index.php?id=82
After I click:
index.php?
As you can see, I want the button to only show an alert, not to change the URL
Full code:
<form>
<label>Title</label><br>
<input type="text" value="<?php echo $title ?>"><br><br>
<label>Description</label><br>
<textarea rows="5" maxlength="120"><?php echo $desc ?></textarea><br><br>
<div>
<?php for($k = 0; $k < count($images); $k++) { ?>
<div>
<img src="<?php echo $images[$k] ?>">
<button onclick="alert('<?php echo $images[$k] ?>')">Click me!</button>
</div>
<?php } ?>
</div>
</form>
PS: I'm not sure what's the problem but I think is the form
There are at least two ways of achieving this:
html:
<button type="button" onclick="alert('<?php echo $images[$k] ?>');">Click me</button>
<button onclick="alert('<?php echo $images[$k] ?>');return false;">Click me!</button>
The first option I think would be best if the only thing you want to achieve is to alert a text.
The second option might be better if call a function when you click on the button and want different responses:
<button onclick="return foo('<?php echo $images[$k] ?>');">Click me!</button>
in javascript:
function foo(image) {
//If image is img1 then update the page
//If any other image, don't update the page
if (image == 'img1') {
return true;
}
return false;
}
When you fail to specify the type of a <button> element, the browser will imply that it is a submit type button. Thus, when you press the button, your browser is submitting the form with it.
You should either,
a) Specify a type for the button other then submit, or
b) Return false in the javascript code to run, to prevent the click event from bubbling.
I have form page calling a function that pulls 2 random names from a mysql database (called by php function pairsim()). I want to create a group of checkboxes that will create conditions that I can use to limit to mysql pulls. These conditions then need to live on until the user changes the configurations (update button below).
I am hoping to create an array of checked values to use as a condition until the user updates his configurations again. Unfortunately, I can't get the array to persist while going page to page. I am also having trouble creating an array to pass back into the pairsim(). Forgive my ignorance, is there anyway to accomplish what I am trying to do?
select_checkbox.php:
<?php
session_start();
$_SESSION['G'] = isset($_POST['pG']) ? 1 : 0 ;
$_SESSION['D'] = isset($_POST['pD']) ? 1 : 0 ;
$_SESSION['W'] = isset($_POST['pW']) ? 1 : 0 ;
$_SESSION['C'] = isset($_POST['pC']) ? 1 : 0 ;
?>
<button class="btn btn-success" type="button" data-toggle="collapse" data- target="#collapselimit" aria-expanded="false" aria-controls="collapselimit">
Limit Names
</button>
<div class="collapse container-fluid" id="collapselimit">
<form role="form" method="post" action=<?php pairsim() ; ?>>
<div class="panel panel-default">
<div class="panel-body">
<p><b>Position Limited:</b><br></p>
<!--create array with checked values to include to mysql function-->
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pG' checked>Goalies</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pD' checked>Defensemen</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pW' checked>Wingers</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pC' checked>Centers</label>
<div>
<!--locks in checkbox configuratons-->
<input type="submit" class="btn btn-success btn-lg" value="Update"/>
<!--script below to check/uncheck all-->
<input type="button" class="btn btn-default btn-lg" id="CheckAll" value="Check All" />
<input type="button" class="btn btn-default btn-lg" id="UncheckAll" value="Clear All" />
</div>
</form>
</div>
</div>
I see a few errors here, I'll try to go in procedural order.
Your checkboxes all use the same name, so the resulting post var would be $_POST['positionsel'];. So your existing session check is always going to be false.
BTW the output var_dump($_POST['positionsel']); with everything checked would look like:
array(4) {
[0]=>
string(2) "pG"
[1]=>
string(2) "pD"
[2]=>
string(2) "pW"
[3]=>
string(2) "pC"
}
Fix:
session_start();
$checkBoxValues = array('pG', 'pD', 'pW', 'pC');
foreach($checkBoxValues as $check)
{
if(in_array($check, $_POST['positionsel']))
{
$_SESSION[$check] = true;
} else {
$_SESSION[$check] = false;
}
}
This will check the $_POST variable for the checkbox values, if it exists that means it was checked.
What this will then cause is a situation where if you do not submit a form, then no post value will process, causing the $_SESSION variables to be wiped. TO prevent this, we need to check if the form was in fact submitted. A cheap way to do that is to name the submit field:
<input name="submitted" type="submit" class="btn btn-success btn-lg" value="Update"/>
And before the foreach, do check for it:
if(isset($_POST['submitted']))
{
$checkBoxValues = array('pG', 'pD', 'pW', 'pC');
foreach($checkBoxValues as $check)
{
if(in_array($check, $_POST['positionsel']))
{
$_SESSION[$check] = true;
} else {
$_SESSION[$check] = false;
}
}
}
Now the session vars will only be overwritten if the form is actually submitted.
Next, you need to change your check boxes to only be "checked" if they were in fact checked:
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pG' <?PHP if($_SESSION['pG']) { echo "checked"; }?> >Goalies</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pD' <?PHP if($_SESSION['pD']) { echo "checked"; }?> >Defensemen</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pW' <?PHP if($_SESSION['pW']) { echo "checked"; }?> >Wingers</label>
<label class='checkbox-inline'><input type='checkbox' name='positionsel[]' value='pC' <?PHP if($_SESSION['pC']) { echo "checked"; }?> >Centers</label>
That should then allow the form to reload with the already configured checkboxes, preserving your preferences across resubmits & new page loads as long as the session stays alive.
In this example I changed your session variable names to the check box values, so keep that in mind if you paste it over as you'll need to adjust it anywhere else you use the session (or adjust my code to use your original varnames).
<?php $result2 = db_getsub( array('campaigns_id' => $SYS_campaign) );
if (!is_null($result1)){
while ( $row = $result2->fetch_object() )
{
?>
<div class="item">
<div style="text-align: left;">
<div class="choicetitle">
<input type="radio" id="Radio1" checked="checked" title="" name="subtype" value="<?php echo $row->subid; ?>" /><?php echo $row->sub_name; ?></div>
<div class="choicedesc descrip">
<?php echo $row->gavdescrep; ?> </div>
<!-- <div class="choicelesmer">
Les mer
</div>-->
<div class="choicedesc1">
<?php echo $row->gavdescrep; ?> </div>
</div>
<span><?php
$value=$row->price;
if($value != 0)
{
echo $row->price; ?>,-
<?php }else { echo ""; } ?> </span>
<span class="bgcolor discprice">
<?php echo $row->pricediscount; ?>,-
</span>
</div>
<?php } } ?>
I want to show default 1st radio button checked now its showing last button checked as it is coming dynamically any help???
You are using checked="checked" for all your radio buttons in the loop. So, last assigned radio button retains the checked property. To solve the problem, Conditionally set checked="checked" only for the first looping
if(loopingFirstTime){
$checked = ' checked="checked" ';
}else{
$checked = ' ';
}
and then ....
<input type="radio" name="radio1" '.$checked.' />
Not tested this .... please check the syntax
Use checked=checked property to make a radio button checked by default:-
Blue
http://www.w3schools.com/jsref/prop_radio_defaultchecked.asp
And to handle that with jquery:-
jQuery("#radio_1").attr('checked', 'checked');
How to check a radio button with jQuery ?
Like the other guys have already said, you have only implemented one radio button.
You can try jsfiddle.net for example to try out you code and quickly see your changes.
Btw: I'm not quite sure what you're trying to do in you last row: Is that a leftover or those this belong to the wrapped-around code?
I have a search function that will accept a search string and send it to a php file for parsing a database column. I'd also like users to choose which aspect of the website they'd like to search (comics, artwork, or both). Comic and Artwork or stored in two separate tables.
This is a function that will accept an input search string from the html below and send it to a php file.
<script type="text/javascript">
function search(searchString) {
//var site = $("#site").val();
$.get("./scripts/search.php", {_input : searchString},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
And this is javascript to accept a choice to search "comics", "artwork" or "all".
function searchChoice(choice) {
alert("Choice: " + choice);
$.get("./scripts/search.php", {_choice : choice}
);
}
</script>
HTML:
<!--Search filtering for comics, artwork, or both-->
<span class="search"><b>Search for: </b> </span>
<div class="btn-group" data-toggle="buttons-radio">
<span class="search">
<button type="button" class="btn" id="comics" onclick="searchChoice(this.id)">Comics</button>
<button type="button" class="btn" id="artwork" onclick="searchChoice(this.id)">Artwork</button>
<button type="button" class="btn" id="all" onclick="searchChoice(this.id)">All</button>
</span>
</div>
<br/>
<br/>
<!--Search functionality-->
<span class="search">
<input type="text" onkeyup="search(this.value)" name="input" value="" />
</span>
<br />
<span id="output"><span class="sidebarimages"> </span></span>
PHP excerpt:
$input = (isset($_GET['_input']) ? ($_GET['_input']) : 0);
$siteChoice = (isset($_GET['_choice']) ? ($_GET['_choice']) : "all");
You can see the javascript correctly alerting out "Choice: comics" when comics button is selected, but the php side, echo "</br>Choice: " . $siteChoice;, is echo'ing out "all", which is incorrect.
Any ideas would be greatly appreciated!
As mentioned #E_p, that is the problem ... another option is to create a variable and store the data there ... try this: you don't need change the html
var mySearchString = 0;
var myChoice = 'all';
function search(searchString) {
mySearchString = searchString;
GetSearch();
}
function searchChoice(choice) {
myChoice = choice;
GetSearch();
}
function GetSearch(){
$.get("./scripts/search.php", {_input : mySearchString, _choice : myChoice},
function(returned_data) {
$("#output").html(returned_data);
}
);
}
You do not keep state for _choice.
When search is called it does not pass it to a server.
You need to change buttons to option and in search function pass both. to a server at the same time
Replace the buttons with radio buttons and use form.Serialize()
<form id="searchform">
<input type="radio" name="_choice" value="comics" />Comics<br/>
<input type="radio" name="_choice" value="artwork" />Artwork<br/>
<input type="radio" name="_choice" value="all" />All<br/>
<input type="text" onkeyup="search()" name="_input" value="" />
</form>
Javascript
function search() {
//var site = $("#site").val();
$.get("./scripts/search.php", $('#searchform').serialize(),
function(returned_data) {
$("#output").html(returned_data);
}
);
}
The .serialize() function converts form input to JSON so you don't have to type manually, No more parameter, and no two functions, just one to do them all
I am populating a list of checkboxes from a foreach loop, and giving each an ID. I have added a set of divs below that would appear, depending on which checkbox is created. I was thinking I could load the id variable into a jQuery if statement, and then use a .toggle to show or hide the corresponding div.
<?php
//Call Programs
$getPrograms = Doctrine::getTable('Program')->createQuery()->where('subsection=?', 'mfa')->orWhere('subsection=?', 'mps')->orWhere('subsection=?', 'mat')->orWhere('subsection=?', 'ma')->orderBy('title ASC')->execute(); ?>
<div class="form_row">
<label>
<span><sup class="required_form_item">*</sup>Select Program</span>
</label>
<div class="buttonColumn" style="margin-left:170px;">
//loop the records in with checkboxes
<?php foreach ($getPrograms as $prog): ?>
<?php
$subsection = $prog->getSubsection();
$subsection = strtoupper($subsection);
$trimProg = trim($prog->getTitle(), ' ');
$removeChars = array(" ", "&");
$trimProg = str_replace( $removeChars, '', $trimProg );
$trimProg = preg_replace('/\s\s+/', '_', $trimProg);
?>
//custin id matches record title
<input type="checkbox" name="program" class="isChecked" id="<?php echo $trimProg; ?>" value="<?php echo $subsection . " " . $prog->getTitle() ?>" /><?php echo $subsection . " " . $prog->getTitle() ?><br />
<?php endforeach ?>
</div>
</div>
The following divs would be set to display:none until the matching checkbox is checked.
<div class="form_row sessionTime" id="Program1" style="display:none;">
Please choose an session time:
<input type="checkbox" name="schedule" value="5:00 pm" />5:00 pm<br />
</div>
<div class="form_row sessionTime" id="program2" style="display:none;">
Please choose an session time:
<input type="checkbox" name="schedule" value="10:00 pm" />10:00 pm<br />
</div>
...
And this is what I thought would work...but alas...it doesn't
$('.isChecked').click( function() {
if ( $(this).is(':checked') ) {
$thisID = $(this).attr("id");
$('.'+ $thisID).show();
}
else {
$('.'+ $thisID).hide();
}
}
);
You should be using "Program1" as class name instead of id like this
<div class="form_row sessionTime Program1" style="display:none;">
Please choose an session time:
<input type="checkbox" name="schedule" value="5:00 pm" />5:00 pm<br />
</div>
And your jQuery code should work, which you can simplify as follows:
$('.isChecked').click( function() {
if ( $(this).is(':checked') ) {
$('.'+ this.id).show();
}
else {
$('.'+ this.id).hide();
}
});
I tested this code # home and it works as you might want
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(
function()
{
$('.isChecked').click(
function()
{
if ( $(this).is(':checked') )
{
$(this).show();
}
else
{
$(this).hide();
}
}
);
}
);
</script>
</head>
<body>
<form>
<input type="checkbox" name="program" class="isChecked"/>
<input type="checkbox" name="program" class="isChecked"/>
<input type="checkbox" name="program" class="isChecked"/>
<input type="checkbox" name="program" class="isChecked"/>
</form>
</body>
I think the problem in your code can come from the $(document).ready() handler that waits for the entire DOM to be loaded before binding action listener to it.
What's more, your jquery code wasn't working for me. My version seems to work, but once checked box hidden, the user cannot handle them anymore.
Otherwise, I think doing some Doctrine requests in your template is a very bad idea.
Good bye !