I want to use PHP variable in jQuery in loop - php

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>

Related

jQuery Get Value From Looping in PHP

I'm using jQuery. Now I want to get value base on ID value from textbox loop.
Example :
<?php
while
{
$id = $d['id'];
?>
<input type="text" name="text" id="txt" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="txt<?php echo $id; ?>" value="Go"/>
<?php
}
and jQuery like this:
$(function() {
$(".btn_confirms").click(function()
{
var txt_id = $('#txt').val();
alert(txt_id);
}
});
Example if I have 3 data.
The problem is, when I click button Go, it just show first value of PHP loop. Another button Go 2 & 3 is show same value.
Any help, please.
You can't have duplicate IDs on the page, they should be unique. For repeating elements either use classes, or make IDs unique (like you're doing for the buttons).
Something like this should work:
<input type="text" name="text" id="txt<?php echo $id; ?>" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="button<?php echo $id; ?>" value="Go"/>
Then, to fetch the id of the input field from the click function, you can do something like this:
$(".btn_confirms").click(function()
{
var txt_id = $(this).attr('id').replace('button', 'txt');
var value = $('#'+txt_id).val();
alert(txt_id + ": " + value);
})
See it here: http://jsfiddle.net/shomz/R9s2R/
you can try this
<?php
while
{
$id = $d['id'];
?>
<input type="text" name="text" id="txt<?php echo $id; ?>" value="<?php echo $id; ?>"/>
<input type="submit" class="btn_confirms" id="btn<?php echo $id; ?>" textbox_id="#txt<?php echo $id; ?>" value="Go"/>
<?php
}
?>
<script>
$(function() {
$(".btn_confirms").click(function()
{
var textbox_id = $(this).attr('textbox_id');
var textbox_val = $(textbox_id).val();
alert(textbox_val);
});
});
</script>
DEMO

PHP Passing Input values through href to another page

<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>

show div on checkbox checked php

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"});
}
});
});

Post form values where Field Names are dynamically created from a loop?

I have a page that is created dynamically from the information that a user has submitted on a previous page. For example on page 1, the user inputs some department names. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. I need to know how to post these items once the submit button in each section is clicked. I have tried several different ways and nothing is working. I want it so that ONLY the items with the same $a value as the submit button's $a get posted.
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a;?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
<?php
echo "</div>";
}
?>
*EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. *
**EDIT 2 :
I figured it out using #dirt 's jquery suggestion.
HTML:
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a ?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>
<?php
echo "</div></div>";
} ?>
jQuery:
<script>
$(document).ready(function() {
$('#<?php echo $a ?>').submit(function() {
.post("include/action.php")
});
});
</script>
PHP:
if(isset($_POST['areaSub'])) {
$areaval = intval($_POST['areaSub']);
$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]);
echo $areaval;
echo $area_name;
echo $bdep;
}
EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button.
Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. Multiple buttons with the same name can have different values and the values don't have to be the labels.
Example:
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
# _POST:
if (isset($_POST['submit']) && $_POST['submit'] == '1') {
echo 'Department 1';
}
if (isset($_POST['submit']) && $_POST['submit'] == '2') {
echo 'Department 2';
}
?>
You could also use jQuery to get all elements contained in a certain Div ID. Something like (this is rough idea):
<div id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>
jQuery:
$('#<?php echo $a; ?>').submit(function() {
do_something_with_form_$a
});
I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? And you only want the input fields for that specific area submitted?
Section off forms with the tag:
<form> </form>
So, you'll have multiple areas:
while(...) {
<form method="..." name="form $a">
<input name="... $a">
<input name="... $a">
...
<submit>
</form>
}
(Obviously this is pseudocode, adjust accordingly)

Passing loop values of child window to parent window and vice versa

I have a text box named subscriber name.when I double clicked on that text box,a child window should open.In that child window list of subscriber names are shown using while loop of mysql query.My problem is when I double clicked on it ll pass a value as undefined.This problems comes due to looping of subscriber names from db table.How can I solve it? Please help me soon.Here my code.
<script type="text/javascript">
function displaymessage(){
opener.document.cash_entry.sub_name.value = document.subscriber.subname.value;
self.close();
}
</script>
<form>
<?php
$sel=mysql_query("select * from add_ticket");
while($row=mysql_fetch_array($sel)) {
$subscriber=$row['subscriber'];
?>
<input type="text" name="subid" id="subid" value="<?php echo $subscriber; ?>"
ondblclick="displaymessage()" readonly="true">
<?php } ?>
</form>
You have not named your form and you named your field differently than in the HTML code and you have more than on element with the same name which makes it an array if more than one
Do this
<script type="text/javascript">
function displaymessage(fld){
opener.document.cash_entry.sub_name.value = fld.value;
self.close();
}
</script>
<form>
<?php
$sel=mysql_query("select * from add_ticket");
while($row=mysql_fetch_array($sel)) {
$subscriber=$row['subscriber'];
?>
<input type="text" name="subname"
id="subid<?php echo $subscriber; ?>"
value="<?php echo $subscriber; ?>"
ondblclick="displaymessage(this)" readonly="true">
<?php } ?>
</form>
by why not
<input type="button" value="<?php echo $subscriber; ?>"
onclick="displaymessage(this)" >
For more than one value, you can do
function displaymessage(fld){
var parts = fld.value.split("|");
opener.document.cash_entry.sub_name.value = parts[0];
opener.document.cash_entry.sub_id.value = parts[1];
self.close();
}
<input type="button"
value="<?php echo $subscriber; ?>|<?php echo $subscriberID; ?>"
onclick="displaymessage(this)" >

Categories