I dont understand why this code doesnt work :
$( "#remove" ).click(function() {
var getcontent = $("#noticeid").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
Heres the html textbox :
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And the button
<input type='button' id='remove' value='remove' class='btn btn-danger'>
And heres the PHP (on the same page):
if($_GET['removeid']){
$removeid = $_GET['removeid'];
$querydelete = "DELETE FROM `$dbtable`.`Notices` WHERE `NoticeID` = $removeid";
mysqli_query($conn,$querydelete);
}
This worked with just simple PHP and HTML so it must be something to do with the script. Sorry if its a really simple mistake but I cant seem to fix it.
Edit:
I was putting the input in a if clause that wasnt working so I fixed that but it still doesnt work, here is the new HTML code ($info7 = notice id ):
<input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='myname' value='$info7'>
You are trying to get a dom element by ID, but only name is set in your html:
<td><input class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
You should add id, e.g.:
<td><input id='noticeid' class='form-control' style='position:absolute;left:500%;' type='text' name='noticeid' value='$editid'></td>
And then you will be able to successfully do:
var getcontent = $("#noticeid").val();
Try getting the value by name as you don't have an id in your input type
$( "#remove" ).click(function() {
var getcontent = $("input[name='noticeid']").val();
$.get("admincp.php", { removeid : getcontent } ,function(){
});
});
You are trying to retrieve
$("#noticeid").val();
But there is no element with this Id in the DOM. Please add id attribute.
<input class='form-control' id="noticeId" style='position:absolute;left:500%;' type='text' name="noticeid" value='$editid'>
If you don't want to add id then you can do like this
var getContent=$("input[name='noticeId']").val();
else
var getcontent=document.getElementByTagName("noticeId").value;
else
var getcontent=document.querySelector("input[name='noticeId']").value;
Related
I have a table where i dynamically add rows with input fields based on the user requirement. These input fields are stored as an array so that looping over them can get the values in each row to insert into the db. The issue is that only the first "static" row from the table is recognized in the array. Every other dynamic input field is not stored in the input array when submitted. A snippet of the table code is
<tr><td><input type=number name=minimum[] required></input></td><td><input type=number name=maximum[] required></input></td> <td><input type=number name=overall[] required></input>
<td><?php echo $formular;?></td></tr>
</tbody><tfoot><tr><td colspan="25"><div class="text-center">
<button type="submit" class="btn btn-success">Submit</button>
</div></td></tr></tfoot></table></div></form>
The jquery code to add an extra row is:
<script>
$(window).load(function(){
$(function () {
var rowv="<tr><td><input type=number name=minimum[] required></input></td>
<td><input type=number name=maximum[] required></input></td><td><input
type=number name=overall[] required></input></td><td><?php echo $formular;?>
</td></tr>";
$("#addRow").click(function () {
var row = $(rowv);
$("#mt > tbody").append(row);
});
});
});
</script>
The (abbreviated) php code to process the array is :
foreach($_POST['fi'] as $key=>$value){
$sql="insert into band(fi) values ('$value')";
}
Only the first row is being inserted. Any row that added dynamically is not submitted to the post array. How can i resolve this?
Create your input field like this and then append it to the form and then try.
var input = document.createElement("input");
input.type = "text";
input.name = "minimum[]";
input.required = "true";
$("#testForm").append(input);
Your jQuery syntax seems incorrect. Change that click function to this:
$("#addRow").click(function () {
$("#mt > tbody").append(
"<tr>" +
"<td><input type='number' name='minimum[]' required></td>" +
"<td><input type='number' name='maximum[]' required></td>" +
"<td><input type='number' name='overall[]' required></td>" +
"<td><?php echo $formular;?></td>" +
"</tr>"
);
});
code:
<script>
$(document).ready(function(){
$(".check").click(function(){
comment = $(".comment2").val();
alert(comment);
});
});
</script>
<?php
$sql = "select * from enquires2";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>
<input type='checkbox' class='check' id='chk".$row['id']."' name='chk' value=".$row['id'].">
</td>
<td>
<input type='text' name='comment2' class='comment2' id='comment2_".$row['id']."' value='' />
</td>
</tr>";
}
?>
In this code when I enter text inside comment2 input field and then check checkbox it alert only 1st row value but when I enter second textbox and then again check checkbox it show nothing. So, how can I get multiple textbox value onclick on checkbox ? Please help.
Thank you
Problem is, that both the elements are of class .comment2, which means the $(".comment2") creates an array for you, and the .val() only gets the value of the first element in the array.
You have to use a more unique value (since ID's not always start at 0, a simple counter thingy wont work)
May I suggest
<script>
$(document).ready(function(){
$(".check").on("click", function(){
var $id = $(this).val();
comment = $("#comment2_" + $id).val();
alert(comment);
});
});
</script>
Using $(this).val() to get the id value saved in your checkbox, and concatenating that with the ID comment2_x to get the content of the input box
I have some PHP code that generates out a bunch of store items from my database. Each item has a quantity text box and an add to cart submit button and a hidden value with the special ID.
Here is basically how my form is generated:
<form class='form-inline' id='addtocart_form' action='
additem.php?iid=$SaleItem_Id&u=".$_SESSION['id']." ' method='post' role='form'>
<div class='form-group'>
<div class='input-group'>
<input type='text' class='form-control' style= 'float: left; width:50%;' id='quantity'
name='quantity' value='0'></input>
<button type='submit' name='add_to_cart' id='add' class='btn btn-success'>Add to
Cart</button>
</div>
<input type='text' name='$SaleItem_Id' style='display: none;' id='$SaleItem_Id'
value='$SaleItem_Id'>
</form>
My cart works perfectly, except it refreshes and puts you back up to the top of the screen. So then I decided to implement jQuery. All of these generated forms have the same id: addtocart_form.
$(function() {
$("#addtocart_form").on('submit' , function(e) {
e.preventDefault();
var thisForm = $(this);
var quantity = $("#quantity").val();
var dataString = $("#addtocart_form").serialize();
$.ajax({
type: "POST",
url: thisForm.attr('action'),
data: dataString,
});
$("#quantity").val("0");
return false;
});
});
The first item that is displayed on the screen works perfectly. It adds the item to the cart without refreshing the screen.
All of the other forms on the page are being submitted without the jQuery. They add the item, but redirect to the URL of my action.
How can I fix this without rewriting my entire store? I assume it has something with which form is being told to submit.
The id attribute should be unique in same document so try to replace the id addtocart_form by class, and all the other id's by classes to avoid duplicated id.
HTML :
<form class='form-inline addtocart_form' action=...
JS :
$("body").on('submit', '.addtocart_form', function(e) {
e.preventDefault();
var quantity = $(this).find(".quantity").val();
var dataString = $(this).serialize();
var action = $(this).attr('action')
$.ajax({
type: "POST",
url: action,
data: dataString,
});
$(this).find(".quantity").val("0");
return false;
});
Hope this helps.
You should not have more than one element with the same id on a page. If all of your forms use the same id, that's a problem.
Since you are using JQuery with AJAX, there's really no need to use a form at all. Just use a regular button (type="button") and tie a click event to it. Find the parent div of the button and get the values of the inputs within that div.
If your markup looks like this:
<div class='form-group'>
<input type='text' class='form-control quantity' style='float: left; width:50%;' value='0'>
<button type='button' class='btn btn-success add'>Add to Cart</button>
<input type='text' style='display: none;' class='saleItem_Id'>
</div>
<div class='form-group'>
<input type='text' class='form-control quantity' style='float: left; width:50%;' value='0'>
<button type='button' class='btn btn-success add'>Add to Cart</button>
<input type='text' style='display: none;' class='saleItem_Id'>
</div>
You can iterate over the inputs within the div like so:
$(".add").on('click', function() {
var parentDiv = $(this).closest("div");
//in this example, you only have one element, but this is how you would iterate over multiple elements
parentDiv.children('input').each(function() {
console.log($(this).prop('class'));
console.log($(this).val());
});
//do your ajax stuff
});
JS Fiddle demo
hi everyone im just started learning ajax.
im trying to get the return value from php using ajax call and display to text fields.
here is my ajax code.
var getitem=$('#selectedItemId2').val();
if (getitem==''){
//do nothing
}else{
// verifying if item code is already been save and put to the text fields
$('#gettingitem').css('display','block');
$('#gettingitem').html();
$.ajax({
type: 'POST',
url: 'veritemcode.php',
datatype: 'text',
data:{'getitem':getitem
},
success:function(data){
window.setTimeout(function(){
$('#gettingitem').css('display','none');
$('#disp').css('display','block');
$('#disp').html(data);
});
}
});}
and this is my veritemcode.php that i want to display on the text fields
$getitem=$_REQUEST['getitem'];
$verifyitem=mysql_query("select * from item_master where item_code='".$getitem."'") or die (mysql_error());
$vernum=mysql_num_rows($verifyitem);
if($vernum!=1){
}else{
while($dispresult=mysql_fetch_array($verifyitem)){
echo $dispresult['item_desc'];
echo $dispresult['sup_item_code'];
echo $dispresult['smalluom'];
}
}
all i want is to display the echo to this fields
<input type='text' id='itemdesc'/>
<input type='text' id='supitem'/>
<input type='text' id='smalluom'/>
Please help me with this problem...
I would do the following...
split the ajax data in php by a special character, for example a pipe:
while($dispresult=mysql_fetch_array($verifyitem)){
echo $dispresult['item_desc'] . "|";
echo $dispresult['sup_item_code'] . "|";
echo $dispresult['smalluom'];
}
then in html/javascript right after success:function(data){ add the following:
var split_data=data.split("|");
$("#itemdesc").val(split_data[0]);
$("#supitem").val(split_data[1]);
$("#smalluom").val(split_data[2]);
ok no need to worry about the code just put the following code in ur file veritemcode.php
<?php
$getitem=$_REQUEST['getitem'];
$verifyitem=mysql_query("select * from item_master where item_code='".$getitem."'") or die (mysql_error());
$vernum=mysql_num_rows($verifyitem);
if($vernum!=1){
}else{
while($dispresult=mysql_fetch_array($verifyitem)){
$ites_descVal = $dispresult['item_desc'];
$supItemVal=$dispresult['sup_item_code'];
$smallomVal=$dispresult['smalluom'];
}
}
?>
<input type='text' id='itemdesc' value="<?=$ites_descVal?>"/>
<input type='text' id='supitem' value="<?=$supItemVal?>"/>
<input type='text' id='smalluom' value="<?=$smallomVal?>"/>
and remove
<input type='text' id='itemdesc'/>
<input type='text' id='supitem'/>
<input type='text' id='smalluom'/>
from ur main page (php/html : whatever)
I am trying to submit a form with ajax. Here is my complete code
<?php
$wparent = "123";
$method = "sms";
?>
<script type="text/javascript">
$(document).ready(function(){
$("#post_<?php echo $wparent;?>").click(function(){
$('#parentpost-<?php echo $wparent;?>').html('Loading.....');
$("#parentpost-<?php echo $wparent;?>").load("<?php echo SITE_URL;?>new_ajax/post_reply.php", {message:$("[name=replynote]").val(), method:$("[name=method]").val(), parent:$("[name=parent]").val()}); //end
}); // end of the main click function
});
</script>
<?php
echo "<textarea name='replynote' ></textarea>";
echo "<input type=\"submit\" class=\"post_button\" id=\"post_$wparent\" value=\"post\" />";
echo "<input type=\"button\" class=\"cancel_button\" value=\"Cancel\" />";
?>
<input type='hidden' name='parent' value='<?php echo $wparent;?>' />
<input type="hidden" name="method" value="<?php echo $method;?>" />
When I checked the post variables with firebug I saw that, its sending only the method correctly. All other values are sent as undefined. I could not find the error till now.
Try pre-populating the data map that you're passing:
$(document).ready(function () {
$("#post_<?php echo $wparent;?>").click(function () {
var message = $("[name=replynote]").val();
var method = $("[name=method]").val();
var parent = $("[name=parent]").val();
var data = {
"message": message,
"method": method,
"parent": parent
};
$('#parentpost-<?php echo $wparent;?>').html('Loading.....');
$("#parentpost-<?php echo $wparent;?>").load("<?php echo SITE_URL;?>new_ajax/post_reply.php", data); //end
}); // end of the main click function
});
If data is an object containing undefined values, then your jQuery selectors aren't working.
Also, since you're only using a name attribute to find your inputs, you may want to be more specific in the selectors.
Finally, the attribute selectors typically need the value of the attribute enclosed in quotes, so try these for your selectors:
var message = $('textarea[name="replynote"]').val();
var method = $('input[name="method"]').val();
var parent = $('input[name="parent"]').val();