i have a list of records from a recordset :
<?php while($row = mysqli_fetch_array($result_imprint))
{ ?>
<tr><td><input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" value="location_<?php echo $row['imprint_location']; ?>"/> </td>
<td><?php echo $row['imprint_location']; ?></td>
<td><?php echo $row['max_size']; ?></td>
<td><?php echo $row['imprint_type']; ?> </td>
<td> <?php echo $row['max_colours']; ?></td>
<td><?php echo $row['setup_fee']; ?></td></tr>
<?php } ?>
I can't figure out how to show a div if someone checks a box from one of these records. there are a max of six records, I have the div's already done out :
<div class="location_1" id="location_1" style="display: none;">
<br /><hr class="style-seven">
Content Here
</div>
This is the code i used but it doesn't seem to work :
$(document).ready(function(){
$('#location').change(function(){
if(this.checked)
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
any help much appreciated.
thanks
For starters... IDs have to be unique on your page. So you cannot have a checkbox with id="location_1" and then have a div with also an id of "location_1"
So have checkboxes with id "location_X" and your divs with "div_location_X"
I would also stop using php on javascript, unless you do a ajax call to a php script or you do a loop to write all the javascript for each of the checkboxes, but it would be simpler to:
when checked (use a class to group all the checkboxes together), you can get javascipt to get the id attribute of the checkbox
<input type="checkbox" id="location_1" name="location_1" class="check"/> </td>
$(document).ready(function(){
$('.check').change(function(){
if((.check).is(":checked"))
var div_name = $(this).attr('id'); //this should come back with location_1 for the first checkbox
$('#div_'+div_name+'').fadeIn('slow'); // prepend div_ for the corrent unique id name
else
$('#div_'+div_name+'').fadeOut('slow');
});
});
Use chrome tools console for debugging
currently your php code will get interpreted as a string.
try changing your code to:
$('#location_['+<?php echo $row["location_id"]; ?>+']').fadein('slow')
and respectively for fade out.
<tr><td>
<input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" class="check"/> </td>
$(document).ready(function(){
$('#location').change(function(){
if((.check).is(":checked"))
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
1.You can't have same id for div and check box,so i am changing div id to location_div
$(document).ready(function(){
$(":checkbox").click(function () {
if($("input[id^='location_']:checkbox:checked").length>0)
{
$("#location_div").removeAttr('style');
}
else
{
$("#location_div").css({"display":"none"});
}
});
});
Related
OK, so is there anyway to get the button id when clicked and store it for use in the next page (i'm trying to use the button id to know which button is clicked and use it for an sql query in the next page) ?
here's my code :
while($donnees=$reponse->fetch())
{
?>
<tr>
<form action="suppression.php" method="post">
<td ><h3 ><a ><?php echo $donnees['Ncheque']; ?></a></h3></td>
<td><?php echo $donnees['Dateremise'];?></td>
<td><a><?php echo $donnees['Client'];?></a></td>
<td><a><?php echo $donnees['Banque'];?></a></td>
<td><a><?php echo $donnees['Motif'];?></a></td>
<td><?php echo $donnees['Dateretour'];?></td>
<td><?php echo $donnees['Datedelivrance'];?></td>
<td><input type="submit" id="<?php echo $donnees['Ncheque'] ?>" class="button" name="delete" value="supprimer" />modifier</td>
</form>
</tr>
<?php
}
$reponse->closeCursor();
?>
You should wrap each button in a <form> element and specify the GET parameter directly in the action then in PHP check which button was clicked using $_GET.
First Page
<form action='otherPage.php?button=Button1'><button>Button1</button><form>
<form action='otherPage.php?button=Button2'><button>Button2</button><form>
Second Page
<?php
if($_GET['button']=="Button1"){
echo "Button 1 was clicked";
} else if($GET['button']=="Button2"){
echo "Button 2 was clicked";
} else {
echo "No button was clicked";
}
You can use HTML5 localstorage.
It stores values in key value pair in your browser.
localStorage.setItem('key', 'value'); // in place of value give id of the clicked button
I want to use PHP variable in jQuery.
I am having a 50 students data in table which having textbox and radio button.
By default textbox is hidden but when I clicked on radio button textbox show in table.
$("input[type='radio']").on('change', function() {
if ($(this).val() == "2")
var id = '<?php echo json_encode($absent); ?>';
//$("input[type='text']").show();
else
$("input[type='text']").hide();
});
PHP code:
<input name="nxtmark<?php echo $row[0]; ?>" type="text" id="nxtmark<?php echo $row['id']; ?>" onblur="onleave(this.name);" maxlength="2" placeholder="Enter Mark" hidden="" />
<div class="noerrordiv" id="dnxtmark<?php echo $row[0]; ?>">Mark must not be blank</div>
</td>
<td>
<div align="justify">
<input type="radio" id="rdopresent" name="rdopresent<?php echo $row['id']; ?>" checked="checked" value="1" />Present
<input type="radio" id="rdoabsent" name="rdopresent<?php echo $row['id']; ?>" value="2" />Absent<br />
</div>
</td>
</tr>
You can show / hide text box without using PHP variable. See below code -
$("input[type='radio']").on('change',function() {
//find input text from the previous 'td' for which name starts with 'nxtmark'
var $textBox = $(this).closest('td').prev('td').find('input[name^=nxtmark]');
//show hide text box as per radio button value
if($(this).val() == "2")
$textBox.show();
else
$textBox.hide();
});
If your Problem is just to use PHP variable in jquery then following examples will help you.
if you have to user a php array in jquery then use json_encode in PHP not in SCRIPT.
<?php
$a = array(1,2,3,4);
$a = json_encode($a);
$b = "hello";
?>
<script>
$(document).ready(function(){
alert("<?php echo $a; ?>");
alert("<?php echo $b; ?>");
});
</script>
<input type="text" name="a" id="a" />
CLICK
Now this is my question that can we pass an input tag value of HTML to 'mk' so that value can be used in the next page for process.
Though i have done my project in a different way but still i want to know wether is it possible to do so .
I have searched a lot but none of the question is same as i got so plz help me out.
And i dnt want to use form so i just want to know can we pass this value using href tag r not.
This is the code i have
<form method="post">
<table align="center" bgcolor="#FFFFCC" border="1">
<tr><th>ID</th><th>PRODUCT</th><th>PRICE</th><th>DATE OF POST</th><th>PHONE NUMBER</th><th>AUTHENTICATE</th></tr>
<?
$sel = mysql_query("SELECT * FROM addunauth WHERE adminauthorize = 'uncheck'") or die("CANNOT FETCH DATA FOR ADMIN " . mysql_error());
if (mysql_num_rows($sel) > 0) {
while ($data = mysql_fetch_array($sel)) {
?> <tr>
<td><? echo $data['id']; ?></td>
<td><? echo $data['product_name']; ?></td>
<td><? echo $data['product_sp']; ?></td>
<td><? echo $data['product_time']; ?></td>
<td><? echo $data['phoneNumber']; ?></td>
<td><input type="text" name="a" id="a" /></td>
<td align="center">
<a href="processadminTask.php?id=<? echo $data['id']; ?>&mk=" >
AUTHENTICATE
</a>
</td>
</tr>
<?
}
} else {
?> <tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>
<? }
?>
</table>
</form>
Now just tell me how to pass the input value
Your variable not parse inside single quotes. your <? echo '$data['id']' ?> should be <?php echo $data['id'] ?>
<input type="text" name="a" id="a" />
CLICK"
<a id="link" href="xxx.php?id=111&mk=">CLICK</a>
Edit:-
simply add one more hidden fields to store id value as well and get value in js and assign to href
<input type="text" name="idval" id="idval" value="100" />
<input type="hidden" name="mk" id="mkval" value="101" />
Js:-
var idval = $('#idval').val();
var mk = $('#mkval').val();
var link = 'xxx.php?id=' + idval + '&mk=' + mk;
$("#link").attr("href", link);
Working Demo
<? echo '$data['id']' ?>
Is wrong PHP Syntax
Use
<?php echo $data['id']; ?>
Like:
CLICK
Your
<? echo '$data['id']' ?>
is not correct. You can actually use something like this
<?= $data['id'] ?>
so you'll have
CLICK"
I think it is possible with javascript:
getElementById('id of html input').value
This will get you the input value and then you can assign it to 'mk'
So you can check with it
Try this
<?php
$data['id'] = 2;
?>
<html>
<input type="text" name="a" id="a" />
CLICK
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script >
$(document).ready(function(){
$('a[href]').click(function(event){
event.preventDefault();
event.stopPropagation();
var inputTagID = $('#a').val();
var hrefVal = this.toString().replace("&mk=", "&mk="+inputTagID);
window.location = hrefVal;
});
});
</script>
</html>
The code below works, but I have the following problems:
is this even the most efficient/elegant solution to get the desired result? If not, can someone point me in the right direction?
on page load jQuery is not reading the value of the MainWidgetName (a select input) on the form... causing nothing to be displayed until a change is made to that field.
how to clear the values and disable the select input of the unused/unassociated fields upon change, or if javascript is disabled?
jQuery:
<script type="text/javascript">
$(document).ready(function()
{
$.viewMap = {
'0' : $([]),
'6' : $('#result1'),
'7' : $('#result2'),
'8' : $('#result3'),
'9' : $('#result4'),
'10': $('#result5'),
'11': $('#result6')
};
$('#mainWidgetName').change(function()
{
// hide all
$.each($.viewMap, function() {$(this).hide(); });
// show current
$.viewMap[$(this).val()].show();
});
});
</script>
HTML:
<table class="simple">
<?php echo $form['mainWidgetName']->renderRow(); ?>
<tr id="result1" style="display:none"><th><?php echo $form[‘widgetName1’]->renderLabel() ?></th><td><?php echo $form['widgetName1']->render(); ?></td></tr>
<tr id="result2" style="display:none"><th><?php echo $form[‘widgetName2’]->renderLabel() ?></th><td><?php echo $form['widgetName2']->render(); ?>t</td></tr>
<tr id="result3" style="display:none"><th><?php echo $form[‘widgetName3’]->renderLabel() ?></th><td><?php echo $form['widgetName3']->render(); ?></td></tr>
<tr id="result4" style="display:none"><th><?php echo $form[‘widgetName4’]->renderLabel() ?></th><td><?php echo $form['widgetName4']->render(); ?></td></tr>
<tr id="result5" style="display:none"><th><?php echo $form[‘widgetName5’]->renderLabel() ?></th><td><?php echo $form['widgetName5']->render(); ?></td></tr>
<tr id="result6" style="display:none"><th><?php echo $form[‘widgetName6’]->renderLabel() ?></th><td><?php echo $form['widgetName6']->render(); ?></td></tr>
<tr><td></td><td><input type="submit" value="Submit" /></td></tr>
</table>
I found a great plugin that achieved the result I was looking for:
http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/
The php and html are pretty simple:
<?php
session_start();
$_SESSION['liked_or_not']=0;
?>
<button class="photo_like" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" ><?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>
jQuery snippet:
<script type="text/javascript">
$(document).ready(function(){
alert("alert one ="+ $('.photo_like').text());
if($('.photo_like').text()=='Like'){
alert("alert two ="+ like_text);
}
});
</script>
The above code gives the ouput as 'alert one =Like' and then after a few invisible line breaks on the alert box a square. How does that square appear there?
And the second alert box does not appear, as the square box is not included in the if($('.photo_like').text()=='Like'){ comparison.
Also Chrome console says :Uncaught TypeError: Cannot read property 'hasPort' of undefined
Explanation?
You should close your button.
<button class="photo_like" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" ><?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?></button>
And use ternary operator can make your code more clear.
<?php $like_or_not = $_SESSION['liked_or_not']==0 ? 'Like' : 'Unlike'; ?>
<button class="photo_like" value="<?php echo $like_or_not; ?>"><?php echo $like_or_not; ?></button>
if you want the value Like and Unlike the set in into the hidden input box like:
<input type="hidden" name="getTxt" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" >
and use something like this:
var getHiddenVal = $('input[name="getTxt"]').val();
// Return the value
its far easy with messing the codes.
$('.photo_like') can return multiple objects. To be sure it returns only one object use $('.photo_like:first')
To get the value of a button i always use val() instead of text().
So your if should be like this: if($('.photo_like:first').val()=='Like'){}