I am trying to have the focus in my isbn text box or the edition text boxafter running a calculation in jquery, but instead it is going to the first button on the page or the sales rank field.
My html is:
<td><label>ISBN</label></td>
<td><input type="text" id="isbn" name="isbn" /></td>
<td></td>
<td rowspan=7><a id="detailURL" href="#" target="_blank"><img id="bookCover" src="../images/imgNotFound.gif" height="200px" /></a></td>
<td rowspan=7><input type="button" id="isIE" name="isIE" value="IE Price Reduction" />
<br/><br/><input type="button" id="isAIE" name="isAIE" value="AIE Price Reduction" />
<br/><br/><input type="button" id="isModHighlight" name="isModHighlight" value="Moderate Highlighting" />
<br/><br/><input type="button" id="isHeavHighlight" name="isHeavHighlight" value="Heavy Highlighting" />
<br/><br/><input type="button" id="isMissingSupply" name="isMissingSupply" value="Missing Supply" />
<br /><br/><input type="button" id="waterDamage" name="waterDamage" value="Water Damage / Poor Cond." />
</td>
<td rowspan=7>
<!-- SUPPLY CHECK TABLE -->
<table id="supply" summary="Check Supply" width="10%" border="1" align="right">
<caption><h3>Check Supply!</h3></caption>
<thead>
<tr>
<th scope="col" class="rounded-isbn">Supply:</th>
<!--<th scope="col" class="rounded-isbn">Who Requires:</th>-->
</tr>
</thead>
<tbody>
</tbody>
</table>
</td>
</tr>
<tr>
<td><label for="quantity" class="label">Quantity</label></td>
<td><input type="text" id="quantity" name="quantity" /></td>
<td></td>
</tr>
<tr>
<td><label for="payPrice" class="label">Price to Pay</label></td>
<td><input type="text" id="payPrice" name="payPrice"/></td>
<td></td>
</tr>
<tr>
<td><label>Edition</label></td>
<td><input type="text" id="edition" name="edition" readonly="readonly"/></td>
<td></td>
</tr>
<tr>
<td><label>Title</label></td>
<td><input type="text" id="title" name="title" /></td>
<td></td>
</tr>
<tr>
<td><label>Sales Rank</label></td>
<td><input type="text" id="salesRank" name="salesRank" readonly="readonly" /></td>
<td></td>
</tr>
<tr>
<td></td><td><input type="button" id="buy" value="Buy" /></td><td></td>
</table>
</form>
The jquery in question is:
if($('#payPrice').val() <= 0 ) {
$(':text')[0].focus();
} else {
$("#edition").focus();
}
When the value of #payPrice is <=0, it is defaulting to the first button id="isIE" and not the isbn field. If the value of #payprice is >0 it is focusing on the sales rank text area, and not edition. I have tried setting a timeout, which did not work as well as $("#isbn).focus();, which didn't work either. I changed it to $(':text')[0].focus(); to see if that would work, but it still went to the first button.
What am I doing wrong here? How do I correct this to make it work as it should?
have you tried
if(parseFloat($('#payPrice').val()) <= 0 ) {
...
}
if parseFloat make no difference try this:
if($('#payPrice').val()<=0){
$("input[name='isbn']").focus();
}else{
$("input[name='edition']").focus();
}
otherwise you could see that example:
function sampledFunction()
{
window.setTimeout(function ()
{
$('#mobileno').focus();
}, 0);
return false;
}
read here
I'd recommend using a better selector:
like $('[type=text]') or $('input:text')
See: jQuery text selector
Here is the final outcome, thanks to JackTurky who pointed me in the right direction.
//focus on either isbn or edition
setTimeout(function() {
if($('#payPrice').val() <= 0 ) {
$("#isbn").focus();
} else {
$("#edition").focus();
}
}, 10);
Related
I want to bring many $_REQUEST from Tag but only $_REQUEDT['editid'] works. how to solve it.
echo "<td><a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>Edit</a></td>";
//when I put $_REQUEST['editid'] in input it works but others not.
<form action="product.php">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="tid" value="<?php if(isset($_REQUEST['editid'])){echo $_REQUEST['editid'];} ?>"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="tname" value="<?php if(isset($_REQUEST['editname'])){echo $_REQUEST['editname'];}?>"></td>
</tr>
<tr>
<td>Qty</td>
<td><input type="text" name="tqty" ></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="tprice" ></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="btn-add">Add</button>
<button type="submit" name="btn-clear">Clear</button></td>
</tr>
</table>
</form>
The URL parameters after the 1st one should be separated by & (not ,)
Change from
<a href=product.php?editid=$id, editname=$name, editqty=$qty, editprice=$price>
to
<a href="product.php?editid=$id&editname=$name&editqty=$qty&editprice=$price">
This is example html code i used. Basically, when I click 'Add New', the new row appear and I typed in the data. But the problem is only the data of last row (if i add 3 rows, it is only inserting 3rd row's data) is inserting into database
currently i used the simple sql query method to insert into database and I don't know how to modify the sql query or codes to make it store all the duplicated rows' data into databse.
If you can help me with this, it'd be most appreciated. Thanks !!
http://jsfiddle.net/2HGdv/13/
Here is how its work on jsfiddle.
one point is I don't use that <? $i ?> beside my variables in my actual html codes because when I add it, I got errors.
<br>
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;">
<input type="text" name="rollno<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="firstname<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="lastname<? $i ?>" />
</td>
</div>
</tr>
</table>
<br><div id="add_new">ADD NEW
</div>
This is the script for duplicating the rows.
$("#add_new").click(function () {
$("#maintable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
You overwrite your data because input attribute name remains the same. You need to make an HTML array that holds each row. Then in your js you will append one row with index the total .rows like var total_rows = $(".rows").length;
<form method="post" action="stack.php">
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[0][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][lastname]" />
</td>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[1][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][lastname]" />
</td>
</tr>
</table>
<div id="add_new">ADD NEW</div>
<input type="submit" value="Save all items" name="saveButton">
</form>
<?php
if (isset($_POST['item']) && is_array($_POST['item'])) {
$items = $_POST['item'];
$i = 0;
foreach($items as $key => $value) {
echo 'Item '.$i++.': '.$value['rollno'].' '.$value['firstname'].' '.$value['lastname'].'<br />';
}
}
?>
So your next row will be the following:
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[2][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[2][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[3][lastname]" />
</td>
</tr>
I new to programming, and have just finished the jquery course on CodeAcademy.I'm currently trying to create a chained select using Jquery's AJAX function to call a php page which runs a query on my database and echoes it out to my main html page.
Currently, I am able to load only my first , the second and third selects do not seem to be working, and i do not know what exactly it is that i'm doing wrong.
Jquery Code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#category").load("AddItemCat.php");
});
$("#category").onchange(function(){
var category=$("#category").val();
$("#subcat").load("AddItemSubCat.php?category="+category);
});
$("#subcat").onchange(function(){
var category=$("#category").val();
var subcat=$("#subcat").val();
$("#subcat").load("AddItemSubCat.php?category="+category+"&subcat="+subcat);
});
My HTML Form:
<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="edititem" enctype="multipart/form-data" method="POST">
<table border='1'>
<tr>
<td colspan="3">
Category:
<select name="category" id="category" ></select>
SubCategory:
<select id="subcat" name="subcat"></select>
Item:
<select id="item" name="item"></select>
</td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" size="30" maxlength="50" required="required"></td>
</tr>
<tr>
<td>Item Price</td>
<td><input type="number" name="itemprice" size="30" min="1" required="required"></td>
</tr>
<tr>
<td>Item Info</td>
<td><textarea name="iteminfo" col="40" rows="10" maxlength="300" required="required"></textarea>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td colspan="2"><input type="SUBMIT" name="Button" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><?PHP if(isset($errormsg)){echo"$errormsg";}?></td>
</tr>
<tr>
<td colspan="3"><font color="#FF0000"></font></td>
</tr>
I would really appreciate it if someone could point out my mistake and give me pointers on rectifying it.Thanks!
Way to much code to help you but since you say that the jQuery part does not work:
$("#category").onchange(function(){
This should be
("#category").on("change", function(){
There is no onchange in jQuery 1.10 (or any other version?). A brief look into the console would have shown you the error I guess. Additionally, you shoud put all your other calls inside $(document).ready as well.
try use page url instead of file name. e.g - url to AddItemSubCat.php
some of my code in addUser.php it pure html
addUser.php
<form action="process/addNewUser.php" method="POST" id="userForm">
<table width="79%" border="0" cellspacing="6" cellpadding="1"
class="tabcont">
<tr>
<td width="47%" align="right">Title:</td>
<td width="53%">
<select name="title"><option value='0'>- - select - -</option>
</select>
</td>
</tr>
<tr>
<td width="47%" align="right">
First Name:</td>
<td width="53%"><input type="text" id="firstname" name="firstName" class="required" /></td>
</tr>
<tr>
<td align="right">Middle Name:</td>
<td><input type="text" id="middlename" name="middleName" class="name" /></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input name="password" value="" readonly="readonly"
id="password" class="required" " /></td>
</tr>
<tr>
<td align="right" colspan="2">
<input name="addNewUser" type="submit" class="submit" id="submit" value="Submit" />
</td>
</tr>
</table>
</form>
addNewUser.php
Here i am doing validations and displaying error messages and if it is success sending him to another page.
But i want to show an error message on addUser.php instead of validations page. Please give me a sample code how i can do it.
addNewUser.php
Add the message in the session
$_SESSION["ErrorMsg"]="Name already exists";
And in your addUser.php , you put the following code in the place you need to show the msg :
if($_SESSION["ErrorMsg"])
{
echo $_SESSION["ErrorMsg"];
$_SESSION["ErrorMsg"]="";
}
Whats the best way to print the meterage1 and product1 posts as a single related string value when i loop through the foreach? So for example, product1 input box value which is related to meterage1 and product2 relates to meterage2.
Here is my form:
<form id="orderform" name"orderForm" action="tomypage.php" method="post">
<a id="add">+</a>
<table id="ordertable" width="533" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td width="33%">Product Code (e.g 66203)</td>
<td width="33%">mtrs sq Required (e.g 10)</td>
<td width="33%">Preview Image</td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode1" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage1" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode2" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage2" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
</tbody>
</table>
<button>Submit</button>
</form>
My current PHP foreach:
if ($_POST) {
foreach($_POST as $key => $value) {
$orderdetails = $key.' '.$value.'m<sup>2</sup><br />';
}
} else {
$orderdetails = 'No Order Details Selected';
}
You can assign form fields to an array, instead of numbering them, this will give you far more flexibility when accessing their values: i.e.
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
Then simply do:
print_r($_POST['meterage']);
Or access the $_POST['meterage'] array as required (i.e. loop through for action on each value).
If you then want to loop through the fieldsets, for related products/meterages, you could us:
$number_of_products=count($_POST['prodcode']);
for ( $i=0; $i<$number_of_products; $i++){
echo $_POST['prodcode'][$i]." has the meterage: ".$_POST['meterage'][$i]."<br/>";
}