Only post first result of while loop - javascript

I am using a while loop to display results from a query.
The while loop is working fine.
In the hidden field I would like to post the values of etj12idsinging to another page.
I am submitting the form from a link using javascript.
The problem is regardless of the row I click, it only posts the first value. What should I do?
while($row= mysql_fetch_array($result))
{
$SIidsinging = $row['etj12idsinging'];
?>
<tr>
<td><?php echo $SIidnumber; ?></td>
<td><?php echo $SIidtype; ?></td>
<TD>
<form method='post' name='form1' id="form1" onclick="submitform()">
<input type="hidden" name='name2' id='name2' value="<?php echo $row['etj12idsinging']; ?>"></a>
<input type="button" value="Edit" class="btn-warning"/>
</form>
</TD>
</tr>
The javascript that I used to submit the form:
function submitform()
{
var name2=$('#name2').val();
window.alert(name2);//it keeps having first result
$('#form1').submit();
}

You can use like below codes:
<?php
while($row= mysql_fetch_array($result)) {
$SIidsinging = $row['etj12idsinging'];
?>
<form method='post' name='form1' id="form1" onclick="submitform()">
<tr>
<td><?php echo $SIidnumber; ?></td>
<td><?php echo $SIidtype; ?></td>
<TD>
<input type="hidden" name='name' value="<?php echo $row['etj12idsinging']; ?>"></a>
<input type="submit" value="Edit" class="btn-warning"/>
</TD>
</tr>
</form>
<?php
}
?>
Now in your result of post, you can get this array, by:
<?php
$names = $_POST[ 'name' ];
?>

Related

Page redirect After Update data with custom code in wordpress

I have put a WordPress code to change the profile. The data is being updated, but after updating it is not redirected to the main page. Now I have to redirect it to another page if it gets redirected, then data not update. How could I do that?
<form method="post" id="adduser" action="<?php the_permalink(); ?>" class="profile-field">
<table class="table table-bordered">
<tbody>
<!--tr>
<td>Name</td>
<td><?php echo $user_details['first_name'][0]; ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo wp_get_current_user()->user_email;?></td>
</tr-->
<p class="form-username">
<label for="first-name"><?php _e('First Name', 'profile'); ?></label>
<input class="text-input" name="first-name" type="text" id="first-name"
value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
</p>
<p class="form-username">
<label for="last-name"><?php _e('User mail', 'profile'); ?></label>
<input class="text-input" name="last-name" type="text" id="last-name"
value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
</p>
<!--tr>
<td>Company Code</td>
<td><?php echo $user_details['companycode'][0]; ?></td>
</tr>
<tr>
<td>Industry</td>
<td><?php echo $user_details['industury'][0]; ?></td>
</tr>
<tr>
<td>Job title</td>
<td><?php echo $user_details['jobtitle'][0]; ?></td>
</tr>
<tr>
<td>Experience</td>
<td><?php echo $user_details['experience'][0]; ?> Years</td>
</tr>
</tbody>
</table-->
<?php do_action('edit_user_profile',$current_user); ?>
<p class="form-submit">
<?php echo $referer; ?>
<div>
<button name="updateuser" type="submit" id="updateuser" class="submit button"
value="<?php _e('Update', 'profile'); ?>">update</button>
<?php //wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="update-user" />
</p>
<!--a class="profileinfo-edit" id ="edit" href="<?php echo site_url();?>/my-profile">Update Profile</a-->
</form>
<?php endif; ?>
See: https://codex.wordpress.org/Action_Reference/profile_update
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
// Redirect
$redirect_url = '/your-redirect/permalink';
wp_redirect( $redirect_url );
exit();
}

Sec0nd Data Accessing error in my view page

I have fetched some 10 records from database and accessed in view page using foreach.But i can only give approve to first record not other records.Why it is not happening.
this is my view page
<tr><tbody>
<?php`enter code here`
if(isset($result))
{
foreach($result as $value){?>
<td><?php echo $name;?> </td>
<td><?php echo $value->date1;?> </td>
<td><?php echo $value->purpose;?> </td>
<td><?php echo $value->amount;?> </td>
<td><?php echo $value->rep_date;?> </td>
<?php if($value->temp==1){?>
<td> Approved </td>
<td><?php echo $value->id;?> </td>
<?php } else {?>
<td>
<form method="POST" action="<?php echo base_url().'Admin/updatestatus';?>">
<input type="hidden" name ="hid" id="hid" value="<?php echo $value->id;?>">
<input type="submit" name="sub" id="sub" class="btn green" value="Approve" >
</td>
<?php }?>
<td> chat </td>
</tr>
<?php } }?>
</tbody>
This is my controller
function updatestatus()
{
$this->load->database();
$this->load->library('session');
$this->load->model('lpmodel');
$frmdate=$this->session->userdata('from');
$todate=$this->session->userdata('to');
$status=$this->session->userdata('stat');
$emp_id=$this->session->userdata('id');
$hidid=$this->input->post('hid');
echo $hidid;
$data=array(
'temp'=>'1');
$query=$this->lpmodel->ad_aprove_data($emp_id,$data,$hidid);
if($query==true)
{ //echo "hi";
?> <script>
alert('Approved');
</script><?php
//$value['result']=$this->lpmodel->selectapproverow($id);
$name=$this->session->userdata('name');
$value['name']=$name;
//$status=$this->session->set_userdata('stat');
$value['result']=$this->lpmodel->ad_selectreimb($emp_id,$status,$frmdate,$todate);
$this->load->view('ad_reimbursement',$value);
}
else
{// echo "error";
?> <script>
alert('Try Again');
</script><?php
$value['new']=$this->lpmodel->fetchname();
$this->load->view('ad_reimbursement',$value);
}
}

jquery how can I get data from html table row

I have the following table:
<table cellspacing="0" cellpadding="0" id="product">
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th colspan="2">Nr products</th>
</tr>
<?php foreach ($productsInStock as $product) : ?>
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button></td>
<input type="hidden" name="hiddenfieldname" class="hidden" value="<?php echo $product->getId();?>">
</tr>
<?php endforeach; ?>
</table>
I need the value for every hidden field but I only get the first :
x = ('.hidden').val() // gives the first value
How can I get the different values after every click on delete button
Simplest solution is pass the ID as parameter to deleteDataTable() function.
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable(<?php echo $product->getId();?>);">Delete</button></td>
The problem is: Context.
When you use
x = $('.hidden')
you get all elements with class 'hidden'. .val() then gets the value from the first.
You need to limit the hidden input to the one on the same row as the delete button.
Unfortunately, your current hidden is not actually inside the table and you have this:
<table><tr>...</tr><tr>..</tr>
<input type="hidden"...>
<input type="hidden"...>
You need to change your markup to:
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td>
<button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button>
<input type="hidden" name="hiddenfieldname" class="hidden" value="<?php echo $product->getId();?>">
</td>
</tr>
or similar so that the input is inside a td.
You can then use relative elements on the delete click:
function deleteDataTable()
{
var x = $(this).closest("tr").find(".hidden").val();
}
or, using the amended html above, use .next but I would keep the above
var x = $(this).next(".hidden").val();
Maybe you are looking for this:
var x=[];
$('.hidden').each(function(){
x.push($(this).val());
});
x should contain all the values.
You can simply use this:
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td>
<button type="submit" value="Delete" class="upload getValue" data-product-id="<?php echo $product->getId();?>">Delete</button>
</td>
</tr>
<script>
$('.getValue').click(function(){
var val = $(this).data('product-id');
});
</script>
You can like that
<table cellspacing="0" cellpadding="0" id="product">
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th colspan="2">Nr products</th>
</tr>
<?php foreach ($productsInStock as $product) : ?>
<tr data-productid="<?php echo $product->getId();?>">
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getCategory(); ?></td>
<td><?php echo $product->getPrice().ProductController::coin; ?></td>
<td><?php echo $product->getNrProducts(); ?></td>
<td><button type="submit" value="Delete" class="upload" onclick="deleteDataTable();">Delete</button></td>
</tr>
<?php endforeach; ?>
</table>
Script
function deleteDataTable()
{
var productIdOfCurrentTR = $(this).closest("tr").data("productid");
}

How to get the value of selected checkbox in jquery and assign it to a json array?

I have a problem, I have little understanding about jquery that's why I am having a hard time with my code. I have a pagination of products. And there are checkboxes on it. And my simple goal is to disable the products if the checkboxes are selected. But I can't do it in one form because I have already another process in my form which is delete products if selected. That's why I am planning to create a form action in my jquery and pass all the product id from my checkbox to a json array. But how can I do that?
Here's a bit of my code
<form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
<table class="list">
<thead>
<tr>
<td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
<td class="left"><?php echo $column_name; ?></td>
<td class="right"><?php echo $column_sort_order; ?></td>
<td class="left"><?php echo $column_status; ?></td>
<td class="right"><?php echo $column_action; ?></td>
</tr>
</thead>
<tbody>
<?php if ($categories) { ?>
<?php foreach ($categories as $category) { ?>
<tr>
<td style="text-align: center;"><?php if ($category['selected']) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" />
<?php } ?></td>
<td class="left"><?php echo $category['name']; ?></td>
<td class="right"><?php echo $category['sort_order']; ?></td>
<td class="right"><?php echo ($category['status'] == 1 ? 'Enable' : 'Disable'); ?></td>
<td class="right"><?php foreach ($category['action'] as $action) { ?>
[ <?php echo $action['text']; ?> ]
<?php } ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="center" colspan="4"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<br />
<div align="right">
<select name="action_select">
<option value="enable">Enable Selected</option>
<option value="disable">Disable Selected</option>
</select>
<input type="button" class="button" id="update_status" value="Update Status" />
</div>
<br />
Here's my jquery sample
<script type="text/javascript">
$("#update_status").on('click', function(){
//how can I get the id of my checkboxes and assign it to a json array
//How can I create a form inside it?
});
</script>
That's all guys I hope you can understand my question. Thanks.
Otherwise you can use the below example code
<script>
$(document).ready(function()
{
$("input[type=checkbox]").click(function()
{
var categoryVals = [];
categoryVals.push('');
$('#Category_category :checked').each(function() {
categoryVals.push($(this).val());
});
$.ajax({
type:"POST",
url:"<?php echo $this->createUrl('ads/searchresult'); ?>", //url of the action page
data:{'category': categoryVals},
success : function(response){
//code to do somethng if its success
}
});
}
}
</script>
try
$('input[name="selected[]"]:checked').each(function() {
console.log(this.value);
});
for more info :- use jQuery to get values of selected checkboxes
Try
var values = $('.list input[name="selected[]"]:checked').map(function () {
return this.value;
}).get();

Array of checkboxes and readonly text fields

I have an array of checkboxes and text fields. what i want to achieve is that when the checkbox is not checked the input type text field is readonly but when the checkbox is checked, the input type text field is no longer read only.
<?php
$rsi = pg_query("Select * From items Order by name;");
while ($ri = pg_fetch_array($rsi, NULL, PGSQL_ASSOC)) {
?>
<tr>
<td align="center"><input id="item" class="input_control" type="checkbox" name="item[]" value="<?php echo $ri['item_no']; ?>"></td>
<td class="qty" align="center"><input id="quantity" type="text" name="qty[]" value="<?php echo $rsview['quantity'];?>"></td>
<td ><?php echo $ri['name']; ?></td>
<td><?php echo $ri['model']; ?></td>
<td><?php echo $ri['brand']; ?></td>
<td><?php echo $ri['item_type']; ?></td>
</tr>
<?php } ?>
Try This
<script type="text/javascript">
function toggleTB(myCheckbox){
if(myCheckbox.checked){
document.myForm.myField.disabled=1
}
else{
document.myForm.myField.disabled=0
}
}
</script>
<form name="myForm">
<input type="checkbox" onClick="toggleTB(this)">
<input type="text" name="myField" value="asdf">
</form>

Categories

Resources