How to save files selected by a list of checkboxes - javascript

I have a list of 3 files with its associated checkboxes. The user select 2 checkboxes and click on save button. A save to folder dialog opens for the user to save the files.
This is my code:
if(!empty($listeDocTypeClientProjet))
{
$iGroups = 0;
while($iGroups < count($listeDocTypeClientProjet))
{
?>
<tr>
<td style='text-align: left;'>
<input type="checkbox" name="docTypeSelect[]" class="files" id="docTypeSelect_<?php echo $listeDocTypeClientProjet[$iGroups]->DOC_TYPE_ID ?>" value="<?php echo $listeDocTypeClientProjet[$iGroups]->DOC_TYPE_ID ?>"> <?php echo $listeDocTypeClientProjet[$iGroups]->DOC_TYPE_NOM ?>
</td>
</tr>
<?php
$iGroups++;
}
}
I dont know how to proceed with the save button. Can anyone pls point me in the right direction.
Here is what ive done so far:
function SaveDocType(client_projet_id, client_id)
{
var fileIds = $('.files:checked').each(function() {
window.open('test3.php?file=' + $(this).next('label').text());
});
}

Related

PHP and Javascript issue. Function return always the last value

I searched this website and couldn't find an answer.
I have a php page that display a dropdown list with 2 choices. When on change, a Javascript execute a function that opens a php page to update my database, sending the ID and another parameter with the URL. The problem is the ID value is always the same (the last of the list). Can you help me please, I am very new in PHP and Javascript. Thank you in advance. Here is the PHP code and the javascript:
<?php
$AvailabilityCondition = "1=1";
if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}
$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;
?>
<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
EDIT
</td>
<td width="100" align="right">
<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >
<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">
<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>
<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>
</form>
<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>
<!-- **************************************************************************************************************** -->
</td>
<td>
!!! DELETE !!!
</td>
</tr>
</table>
</div>
<?php
}}
/* free result set */
$rentalresult->free();
/* close connection */
$link->close();
?>
<br/><br/>
<div align="center">
<b>Back to Managers Main Menu</b>
</div>
</body> ​
I just posted more of my PHP page. So you can see the query and the WHILE, where $rowrentals["id"] is coming from.
Here is also a screen capture of how the page looks like: screen capture
I echoed the $rowrentals["id"] under each availability dropdown.
But whatever row I chose to change the availability, it always pass Id=9, the last one. That is what confuses me.
Before the screen capture, all five rows where "Available". Then I selected "Rented" on the first row. The page updated and since Id always =9, you can see the last row "Rented".
The Javascript is working to retrieve the value of the selected item. Because it opens this page perfectly: status-update.php?rented=Yes&Id=9
But again, Id is always 9...
Try with this:
<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
}
If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.
Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.
Here is the code:
$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
$counter =0;
if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;
$counter = $counter+1;
?>
<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
EDIT
</td>
<td width="100" align="right">
<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >
<select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">
<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>
<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select>
</form>
<script type="text/javascript">
function showSelected<?php echo $counter ;?>(val){
document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script> ​

Pass button value as variable of an upload file

I am quite new to javascript and i need some guidance. I have a table that displays some data, the source code of the table is this.
function user_clients_table_storagefolder() {
$con = mysql_connect("localhost","root",'');
if(!$con){
die("Cannot Connect" . mysql_error());
}
mysql_select_db("client_app",$con);
$get_user_clients = "SELECT `ID`,`Name`,`SurName`,`storagefolder` FROM `clients` ";
$clients = mysql_query($get_user_clients,$con);
echo "<table class=table table-condensed>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>SurName</th>
<th>Recipient</th>
</tr>
</thead>";
while($record = mysql_fetch_array($clients)){
echo "<action=pushnotification.php method=post>";
echo "<tr>";
echo "<td>".$record['ID']." </td>";
echo "<td>".$record['Name']." </td>";
echo "<td>".$record['SurName']." </td>";
echo "<td>"."<button type=button id=contact class=btn btn-primary value=".$record['Name'].">Primary</button></td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
//function that is used to display the table of all the clients and fetch back the storagefolder of each user
}
Every table row has a button in the last column that has a different value. When i click any of the buttons a pop up form is displayed asking to upload a file. The source code of the form is the following:
<div id="contactForm">
<p><h4><font color="white"><i>First Choose the clients and then the file which will be uploaded in order to proced</i></font></h4></2>
<p> </p>
<input type="file" class="upload" name="onstorage" id="upload_file" size="50" name="icon" onchange="loadFile(this);" >
<hr>
<div id="myProgress">
<div id="myBar"></div>
</div>
</div>
The code that launches the pop up is the following:
<script>
$(function() {
// contact form animations
$('#contact').click(function() {
$('#contactForm').fadeToggle();
})
$(document).mouseup(function (e) {
var container = $("#contactForm");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.fadeOut();
}
});
});
</script>
Each time i choose a different file to upload the function loadfile() is called that is sending the input value that i have upload.My question is, how can i also pass the value of the button that has been pressed in the same function?
Can someone please help me?
Thanks in Regards

while loop set only first value in input field in php

I want to add users present in a given table. I am iterating whole table and sending each value to javascript file.
<?php
$sql = "select * from user where user_id not in (select second_user_id from friends where first_user_id = '$user_id'\n"
. " union\n"
. " select first_user_id from friends where second_user_id = '$user_id') limit 20";
$result_not_friends=mysqli_query($dbc,$sql)
or die("error in fetching");
// print_r($row_not_friends);
?>
<table class="table table-hover table-bordered">
<h1>Users</h1>
<tbody>
<?php
while ( $row_not_friends = mysqli_fetch_array($result_not_friends))
{
if ( $row_not_friends['user_id'] != $user_id )
{
?>
<tr>
<td>
<?php echo $row_not_friends['user_name']; ?>
</td>
<!-- here I am sending request and processing it via ajax -->
<td><i class="fa fa-user-plus send_request"></i></td>
<input type="hidden" class="send_second" value="<?php echo $row_not_friends['user_id']; ?>">
<input type="hidden" class="send_first" value="<?php echo $user_id; ?>">
</tr>
<?php
}
}
?>
</tbody>
</table>
Now I am accessing each value in a javascript file as follow:
// Here a request is send
$('.send_request').on('click',
function (e) {
e.preventDefault();
var first = $('.send_first').val();
var second = $('.send_second').val();
alert('firt id is ' + first);
alert('second id is ' + second);
$.ajax(
{
url:'send_process.php',
type:'POST',
dataType:"json",
data: { first: first, second: second },
success:function(data)
{
if(data.send_success)
{
window.location.href = "friend.php";
}
else
{
alert("something went wrong");
window.location.href = "friend.php";
}
},
error : function() { console.log(arguments); }
}
);
});
But here var second = $('.send_second').val(); gives only top-most element value of $row_not_friends['user_id'] . When I am echoing the value, it gives correct result.
Please help me.
Because you are selecting ALL the elements in the page and the default behavior of val() is it returns the first item. It has no clue you want the nth item.
First thing is you need to fix your HTML it is invalid. You can not have an input as a sibling of a tr element. You need to move it inside of a TD.
<!-- here I am sending request and processing it via ajax -->
<td><i class="fa fa-user-plus send_request"></i> <!-- removed the closing td from here -->
<input type="hidden" class="send_second" value="<?php echo $row_not_friends['user_id']; ?>">
<input type="hidden" class="send_first" value="<?php echo $user_id; ?>"></td> <!-- moved the closing td to here -->
</tr>
You need to find the elements in the same row as the button you clicked. Since the hidden inputs are npw siblings of the button you can use the siblings() method.
var btn = $(this);
var first = btn.siblings('.send_first').val();
var second = btn.siblings('.send_second').val();

Javascript + PHP - How to check a checkbox when user select a file to upload

I have an input file list from database:
<?php
for ($x=0; $x<count($Permisos); $x++) {
?>
<tr>
<td> <?php echo utf8_encode($Permisos[$x]['des_permiso']); ?></td>
<td> <input name="txt_arch_<? echo $x;?>" type="file" class="text" id="txt_arch_<? echo $x;?>"> </td>
<td> <input name="cbx_<? echo $x;?>" type="checkbox" class="text" id="cbx_<? echo $x;?>" value="S">
</td>
</tr>
<?php
}
?>
This code returns a list of 'permisos' (requirements) with a checkbox next, for check it to upload the file.
What i have to do is: when user select a file (id or name: "txt_arch_<? echo $x;?>"), the checkbox ("cbx_<? echo $x;?>") will check it automatically.
I don't know if if explain very well.
Thank you for answers.
Add this to the document.ready event, assuming your using the jQuery framework:
$('input[type="file"]').on('change',
function()
{
$(this).next('input[type="checkbox"]').prop('checked', true);
}
);
I solved it:
// .....
<input onChange="VerifyFctn('<?php echo $x; ?>')" name="txt_arch_<? echo $x;?>" type="file" class="text" id="txt_arch_<? echo $x;?>">
// .....
Javascript:
function VerifyFctn(id){
document.getElementById("cbx_"+id).checked = true;
}
Anyway, thank you for see the post :)
Greetings

Disable a table or a submit button when clicked and submit the form

I am creating a dynamic table with a query on my BD, the problem is that I want to disable a button on a table every time I make a submit,
I would like to get the form submitted without page load so that the disabled button is visible.
<?php
$theCounter=0;
while($row = mysql_fetch_object($result))
{
?>
<form id="id" action="table.php" method="post">
<input type="hidden" value="<?php echo $row->id_table; ?>" name="idUser">
<tr>
<td><?php echo $row->id_userS; ?></td>
<td><?php echo $row->username_s; ?></td>
<td><?php echo $row->num_people; ?></td>
<td><?php echo $row->start_time; ?></td>
<td>
<input type="submit" name="delete_check" value="Eliminar">
</td>
<td>
<input type="submit" name="push_check" value="Enviar Push">
</td>
</form>
<?php
$theCounter++;
}
?>
The button I want to disable or hide its the one named "push_check".
It doesn't matter if I disable the button or hide the button
Thanks in advance!
If you want the button to be disabled on all the page loads after the first submit then you need to store some flag in the session which will indicate that the button has already been clicked. And you need to check for that flag on every page load.
PHP:-
<?php
$theCounter=0;
while($row = mysql_fetch_object($result))
{
Echo "
<form id='id' action='table.php' method='post'>
<input type='hidden' value='$row->id_table;' name='idUser'>
<tr>
<td>$row->id_userS</td>
<td>$row->username_s</td>
<td>$row->num_people</td>
<td>$row->start_time</td>
<td>
<input type='submit' id='del$thecounter' name='delete_check' value='Eliminar' onclick='hide(del$thecounter)'>
</td>
<td>
<input type='submit' id='push$thecounter' name='push_check' value='Enviar Push' onclick='hide(push$thecounter)'>
</td>
</form>
";
$theCounter++;
}
?>
Now use Javascript:-
function hide(a)
{
document.getElementById(a).style.visibility="hidden";
}
I'm not sure if I understood everything you want to achive, but here is what I made out of your question:
<script>
document.forms[0].onsubmit = function(e){
e.preventDefault(); // at the beginning stop page from being reloaded by the script
var form = e.currentTarget; // get form
form.push_check.disabled = true; // set submit-button disabled
if(form.idUser.value){ // send data if there is data to send
document.forms[0].onsubmit = null; // set event listener null otherwise onsubmit is a forever-loop
document.forms[0].submit(); // send finally data to the php-script
}
else{
form.mysubmit.disabled = false; // there was no data to send
}
}
</script>
You can put this little script after or within the body-tag. It should work in each browser.
One more method to prevent loading the page is using target
<form action='' method='POST' target='upload_target' onsubmit='hide(dynamicid)'>
fields between form.....
<input type='submit' id='dynamicid'/>
<iframe id='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px solid #fff;'></iframe>
</form>
Hi i manage to do it by this:
<?php
$push_button = $row->time_push;
if($push_button==0)
{
?>
<td>
<input type="submit" id='push$thecounter' name="push_check" value="Enviar Push">
</td>
<?php
}
?>
Since i've already making a query to my DB, and the button is making a change, im checking if that change its already made, so if not, im displaying the button! thanks a lot for all your comments!

Categories