Updating table rows using Jquery using select dropdown - php

I am trying to update some table rows (there can be 40-50 rows) generated from a MYSQL query with a Approve/Rejected column at the end of each row, I would like to use jQuery to update the row using a select dropdown, the problem I am having is that it only updates the first row.
I have tried to look at other Stackoverflow threads, there seems to be quite a few with similar issues, but I am still trying to get my head around jQuery so I couldn't work out how to do it.
I am guessing it's because of the same class names?
My HTML table/forms
<table>
<tr>
<td>1</td>
<td>Ren</td>
<td>
<form method="post" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" name="id" value="1">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
<tr>
<td>2</td>
<td>Stimpy</td>
<td>
<form method="post" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" name="id" value="2">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
</table>
jQuery:
$('.save_button').click(function() {
var approval = $('.approval').val();
var id = $('.id').val();
$('.save_status').text('loading...');
$.post('updateRow.php',{
approval: approval,
id: id
}, function(data) {
$('.save_status').text(data);
}
);
});
PHP File:
if(isset($_POST['approval'],$_POST['id'])) {
$approval = $_POST['approval'];
$id = $_POST['id'];
if($approval !="" && $id !="") {
$pdo->ApproveOrDeny($approval, $id);
} else {
echo "The same thing we do every night, Pinky - try to take over the world!";
}
}
Thanks in advance.

You need to access the clicked form values .instead of just using class name
1) Setup unique name for each form
2) Access the value using form name like this $('form[name="form1"]>.id').val()
$('.save_button').click(function() {
var form = $(this).parents('form:first').attr('name');
alert(form);
var id = $('form[name="'+form+'"]>.id').val();
var option =$('form[name="'+form+'"] .approval').val();
alert(id);
alert(option);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>Ren</td>
<td>
<form method="post" name="form1" action="" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" class="id" value="1">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
<tr>
<td>2</td>
<td>Stimpy</td>
<td>
<form method="post" name="form2" class="dangermouse">
<div class="form-group">
<select name="approval" class="approval">
<option value="0">Approved</option>
<option value="1">Reject</option>
</select>
</div>
<input type="hidden" class="id" value="2">
<input type="button" name="updaterow" class="save_button" value="Update">
</form>
</td>
</tr>
</table>

Related

Dropdown Menu and PHP

I am new in coding. I started learn PHP and I got into a problem that I tried to catch the data from dropdown menu and print it inside the same code via PHP. But the problem is when I try to select it doesn't show anything. Please help!
<?php
if (isset($_POST['submit'])) {
$code = $_POST['coder'];
echo "You are ".$code." coder";
}
?>
<form action="" method="post" name="myForm" id="myForm">
<table>
<tr>
<td>Languages: </td>
<td>
<select name="coder">
<option>Select One</option>
<option value="JAVA">JAVA</option>
<option value="PHP">PHP</option>
<option value="C#">C#</option>
<option value="C++">C++</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value ="Submit">
<input type="reset" value="Clear">
</td>
</tr>
</table>
</form>
isset($_POST['submit']) becoming false because there is no any element with name "submit". Try setting name to submit button as.
<input type="submit" value ="Submit" name="submit">

PHP: cannot retrieve data from radio button POST

I'm having issues with retrieving data from the radio button. I can retrieve other data from the other html tags but in radio button I cannot. Why is that? Can you help me?
My HTML code is here:
<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
<input type="hidden" name="action" value="mark_payment">
<input type="hidden" name="client-id" value="{$details[row]->id}">
<input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
<input type="hidden" name="period" value="{$details[row]->period}">
<table width="100%" border="0" style="padding-top: 10px;">
<tr align="center">
<td class="radiobut">
<input type="radio" id="choice1" name="payment_status" value="paid"> Paid</td>
<td class="radiobut">
<input type="radio" id="choice2" name="payment_status" value="declined"> Declined</td>
</tr>
<tr align="center">
<td colspan="2">
<div class="paddingRow3">
<select class="selectbut" id="payments" name="payment-method">
<option value="Cheque">Cheque</option>
<option value="Deposit">Deposit</option>
<option value="Cash">Cash</option>
</select>
</div>
</td>
</tr>
<tr align="right">
<td colspan="2">
<div class="paddingRow3">
Update
</div>
</td>
</tr>
</table>
</form>
While my PHP script is here:
if($_REQUEST['action'] == 'mark_payment'){
echo '<pre>';
print_r($_REQUEST);
exit;
}
My output will be just like this:
Array
(
[NONCE] => f305790c4d8b060121b99fe84a8fdf1a62321b3b06b9097caa8439e2f9c5bae7
[action] => mark_payment
[client-id] => 2699422
[invoice-id] => 13008351
[period] => 11
[payment-method] => Cheque
)
You have duplicate name attributes of payment_status for your two choices. They need to be different, or only the last input element (#choice2) will be available.
I've gone with payment_status_paid and payment_status_declined in the following example, but you can also use the square bracket notation name="payment_status[]" to make an array of POST data.
<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
<input type="hidden" name="action" value="mark_payment">
<input type="hidden" name="client-id" value="{$details[row]->id}">
<input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
<input type="hidden" name="period" value="{$details[row]->period}">
<table width="100%" border="0" style="padding-top: 10px;">
<tr align="center">
<td class="radiobut">
<input type="radio" id="choice1" name="payment_status_paid" value="paid"> Paid</td>
<td class="radiobut">
<input type="radio" id="choice2" name="payment_status_declined" value="declined"> Declined</td>
</tr>
<tr align="center">
<td colspan="2">
<div class="paddingRow3">
<select class="selectbut" id="payments" name="payment-method">
<option value="Cheque">Cheque</option>
<option value="Deposit">Deposit</option>
<option value="Cash">Cash</option>
</select>
</div>
</td>
</tr>
<tr align="right">
<td colspan="2">
<div class="paddingRow3">
Update
</div>
</td>
</tr>
</table>
</form>
To find which one is chosen, use a conditional:
if($_REQUEST['action'] == 'mark_payment'){
echo '<pre>';
print_r($_REQUEST);
if ($_POST['payment_status_paid']) {
// Payment was successful
}
else {
// Payment was declined
}
exit;
}
Hope this helps!

How to get two forms on one submit [duplicate]

This question already has answers here:
How to submit 2 forms in one page with a single submit button
(4 answers)
Closed 7 years ago.
I have two forms that look like this:
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
</form>
<!-- some more HTML -->
<h2>Second form</h2>
<form id="second-form">
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Note that the first form can contain more rows; the user can add more rows to the form. As you can see, there's only one submit button. That's because both forms have to be submitted together. In jQuery I'd do something like this:
$("#second-form").submit(function(event) {
$("first-form").submit();
event.preventDefault();
});
As the front-end developer of the team, I need to pass down the values of both forms to the back-end. Unfortunately I'm at loss how to do this. I figured I could do something like this:
$("#second-form").on("submit", function(event) {
$("#first-form").submit();
$.post("php/do.php", $("#second-form").serialize());
event.preventDefault();
});
$("#first-form").on("submit", function(event) {
$.post("php/do.php", $("#first-form").serializeArray());
event.preventDefault();
});
.serializeArray() seems necessary because of the array-like values in the HTML for #first-form. In the second form, however, we don't need an array. The issue I'm having now is, what code do I need in do.php that can handle the input. How do you access the data, i.e. the serialized forms in PHP?
I tried a number of things such as
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data);
?>
But all of these return an Undefined index error, which seems to mean that my "selector" is wrong. How would I go about selecting the array from the first form, and the values of the second form in PHP? And is my jQuery method of submitting first-form together with the second correct?
Why don't you do this :
<h2>First form</h2>
<form id="first-form">
<table>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
<tr>
<td>
<input type="text" name="left[]" value="">
</td>
<td>
<input type="text" name="right[]" value="">
</td>
</tr>
</table>
<!-- some more HTML -->
<h2>Second form</h2>
<label>
<input type="search" name="sentence" value="i like the bananas">
</label>
<label>
<input type="text" name="parse-as" value="S">
</label>
<label>
<select name="include-features">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<label>
<select name="show-earley">
<option>Yes</option>
<option selected="selected">No</option>
</select>
</label>
<button type="submit">Parse</button>
</form>
Maybe it's not the cleanest way to do it, but it's the easier. Just get the results with :
<?php
$data = parse_str($_POST["right[]"]);
var_dump($data);
$date1 = parse_str($_POST["right[][0]"]);
var_dump($data1);
$data2 = parse_str($_POST["right[][1]"]);
var_dump($data2);
?>

Unable to post values on a page using jquery ajax post method in wordpress custom plugin

I am new to wordpress and i have just created my first ever plugin. This plugin contains a form. Whenever i make jQuery AJAX Post Request to submit the form, i get this error in alert box.
Call to undefined function add_shortcode() in <b>J:\xampp\htdocs\wordpress\wp-content\plugins\auc_result_fetcher\includes\search.php</b> on line <b>22</b><br />
I am using Wordpress 3.8.1. The add_shortcode() function successfully displays the form wherever i want to be displayed. But whenever i make jQuery AJAX Post Request, i get the above error message.
EDIT
My Code Snippet.
add_action('init', function() {
add_shortcode('search_form', 'print_search_form');
});
add_action('init', 'fetch_grades');
add_action('init', 'fetch_search_options');
function print_search_form(){
?>
<div class="auc-search-form">
<div id="error">
<div id="select-class-error"></div>
<div id="select-search-by-error"></div>
<div id="search-field-error"></div>
</div>
<form id="searchForm" method="POST" action="">
<table>
<tr>
<td style="text-align:right;"> <span>Class:</span> </td>
<td><select id="select-class" name="search-class">
<option>Select Class</option>
<?php
$grades = fetch_grades();
foreach($grades AS $v){
echo "<option>".$v->grade_title."</option>";
}
?>
</select>
</td>
<td id="class-select"></td>
</tr>
<tr id="previous-row">
<td style="text-align:right;"> <span>Search By:</span> </td>
<td>
<select id="search-by" name="search-by">
<option>Select Choice</option>
<?php
$grades = fetch_search_options();
foreach($grades AS $v){
echo "<option>".$v->search_title."</option>";
}
?>
</select>
</td>
</tr>
<tr id="search-row">
<td id="search-by-option-label" style="text-align:right;"><span></span></td>
<td id="search-by-field"><input type="text" name="searchBy" id="search" /></td>
</tr>
<tr >
<td ></td>
<td>
<input type="hidden" id="url" name="url" value="<?php echo SEARCH_RESULTS; ?>">
<input type="button" id="search-button" value="Search" />
<input type="reset" name="searchBy" id="reset" value="Cancel"/>
</td>
</tr>
</table>
</form>
</div>
<?php
}
Please help me out. Thanks.

Issue Posting data

I am trying to post the name="pricetag" and name="size" which is in the item and price... I know this is not working because I have use echo but nothing is coming out but if I echo the pid of the product I shows. The codes below
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td name="pricetag" id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td name="size" id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this, this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
click here for live code http://jsfiddle.net/J4rXX/5/ by Ghillied
It doesn't POST because forms don't submit contents from TD elements. Only from INPUTs, SELECTs and TEXTAREAs.
More information on HTML FORMs here. If you want to avoid displaying the same data twice, you should try INPUT type=hidden.
<table>
<tr>
<td>Pricetag</td><td>PRICE_HERE</td>
</tr>
</table>
<form>
<input type="hidden" name="pricetag" value="PRICE_HERE" />
...
</form>
This should do nothing to hurt your existing JS scripts.
The only information that is posted to the server from a HTML form are elements such as INPUT, SELECT, and TEXTAREA.
If you want to post additional data, but make it un-editable to the user, you need to ideally put the data into INPUT elements which have the attributes type="hidden". This will post the data from the field but make them hidden to the user posting the form.
Adding the elements anywhere in the form will work. I've positioned them at the bottom of the form element, with the already existing hidden field, in this snippet.
I've also removed the name attributes from the TD elements in the snippet, to conform with web standards.
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td id="pricetag">£25.00</td>
</tr>
<tr>
<td width="160">Item:</td>
<td id="item">12 inch</td>
</tr>
<tr>
<td width="160">Length*:</td>
<td>
<select name="length" onChange="addCharge(this,this.selectedIndex);" id="priceDropDown" class="small">
<option value="£25.00">12 inch</option>
<option value="£30.00">14 inch (+£30.00)</option>
<option value="£35.00">16 inch (+£35.00)</option>
<option value="£40.00">18 inch (+£40.00)</option>
<option value="£45.00">20 inch (+£45.00)</option>
<option value="£55.00">22 inch (+£55.00)</option>
<option value="£60.00">24 inch (+£60.00)</option>
<option value="£70.00">26 inch (+£70.00)</option>
<option value="£80.00">28 inch (+£80.00)</option>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" id="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pricetag" value="£25.00" id="hiddenpricetag" />
<input type="hidden" name="size" value="12 inch" id="hiddensize" />
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
EDIT:
Just noticed that the form uses javascript, upon the change of the SELECT data, to change the TD elements HTML data.
To make sure that the form posted the correct data that is in the TD elements, you would need to add ID tags to the hidden input fields so that you can program the javascript to update these as well as the TD elements.
I've added the IDs hiddenpricetag and hiddensize to the two hidden fields.
Your javascript function should be somewhat like this:
function addCharge(select, elemIndex)
{
var item = select.getElementsByTagName('option')[elemIndex];
var price = document.getElementById('pricetag');
var sizeDOM = document.getElementById('item');
var hiddenpricetag = document.getElementById('hiddenpricetag');
var hiddensize = document.getElementById('hiddensize');
var finalPrice = "";
/* Getting the price and showing it up */
finalPrice = parseFloat(item.text.substring(11, item.text.length-1));
/* We set a default £25.00 value */
if(isNaN(finalPrice)) finalPrice = "25";
/* we add decimal */
price.innerHTML="£"+finalPrice+".00";
/* Getting the size and showing it up */
var size = item.text.substring(0,7);
sizeDOM.innerHTML = size;
/* Altering the hidden field values */
hiddenpricetag.setAttribute("value", "£"+finalPrice+".00");
hiddensize.setAttribute("value", size);
}
JSFiddle can be found here: http://jsfiddle.net/J4rXX/7/

Categories