Ajax pass prompt input to php - php

A form is dynamically created with PHP loop and presents one row from DB. I'm asking a user for input through prompt when he changes qty, but when I send it with AJAX to PHP it's empty. I guess it's because hidden input "new_message" is filled at the same time AJAX is sending values to PHP script so at that moment is blank. For the first row generated I get a message, but not for any other row.
Can you help me how to pass prompt input to PHP script?
//Update qty on article
$('.update_qty').on('change', function() {
var message = prompt("Upišite razlog za izmjenu količine:");
//e.preventDefault();
//Get message value
$('#new_message').val(message);
var data = $('#form1').serializeArray();
if (message != "" || message != NULL) {
$.ajax({
type: 'POST',
url: 'update_qty.php',
data: data,
success: function(data) {
alert(data);
}
});
} else {
alert("Empty");
e.PreventDefault();
return false;
}
});
<tbody>
<?php if (!empty($sales_plan_list)) {
foreach($sales_plan_list as $sales_key => $sales_value) {
?>
<tr class="new">
<td>
<a href="view_product_sales.php?id=<?php echo $sales_value['article_id']; ?>">
<?php echo $sales_value['article_no']; ?>
</a>
</td>
<td><?php echo $sales_value['description']; ?></td>
<td><?php echo myNumberFormat($sales_value['rrp']); ?></td>
<td>
<form name="form1" id="form1" method="POST" action="update_qty.php">
<input type="text" name="update_qty" class="update_qty" id="qty" value="<?php echo $sales_value['qty'] ?>"><?php echo ' Kom'; ?>
<input type="hidden" name="article_id" id="article_id" value="<?php echo $sales_value['article_id']; ?>">
<input type="hidden" name="sales_plan_id" id="sales_plan_id" value="<?php echo $sales_plan_id; ?>">
<input type="hidden" name="product_mix_id" id="product_mix_id" value="<?php echo $product_mix_id; ?>">
<input type="hidden" name="new_message" id="new_message" value="">
</form>
</td>
<td>
</td>
</tr>
<?php
}
}
?>
</tbody>

The problem is that you have multiple elements with the same id. You should NOT do that. You check the value in jQuery and then send different form data.
This is all just quessing, I am unable to check it. But try changing id to class and make sure that you send the right form data.
For example change this
<form name="form1" id="form1" method="POST" action="update_qty.php">
to this
<form name="form1" id="form<?= $sales_key; ?>" method="POST" action="update_qty.php">

Related

Change only the clicked button to an new one

I have a button inside a table row which changes with a boolean value.
<tr id="<?php echo $zeile['id'] ?>" value="<?php echo $zeile['id'] ?>" class="task">
<td >
<form method="POST" action="main.php" value="<?php echo $zeile['id']; ?>">
<?php if ($done == true): ?>
button type="submit" name="checkbtn" id='<?php echo $zeile['id']; ?>' >
Check
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php else: ?>
<button type="submit" name="uncheckbtn" id='<?php echo $zeile['id']; ?>'>
UnCheck
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<?php endif ?>
</form>
</td>
The boolean value is change after the button is clicked here for ('checkbtn' and 'uncheckbtn')
$done = true;
if(isset($_POST['checkbtn'])){
*sqlvariables*
$done = false;
*sql*
}
with my jquery I want that I can click anywhere on the row to do a button click:
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});
The Problem is that every button changes to unchecked if I click on one row.
I think that I need to work with id's, but I don't know how.
You already have click event handler on tr so no need to use closest, just use find button
$(".table tr").on('click', function() {
$(this).toggleClass('highlighttask');
$(this).find('button[name="checkbtn"]').click();
});

access object method in a view within a loop php7

I'm pulling an array of objects from my database and iterating through them using PHP to display a table. Each result row of the table has the option of deleting that particular record from the database. Each row is also an object, and the object type has a method that will delete itself from the database.
The problem I'm having is thinking through how to get this to work in the view. Right now if you click the delete button, it sets a POST variable to "delete" and reloads the same page wherein a listener process detects the variable and initiates the delete via a hidden input that contains the records database id.
This is all well and good, but these are all objects. Shouldn't each object be able to access it's own methods? It seems the only barrier to this is the interplay between PHP script and HTML forms. My code is below. Ideally, when the delete button is pressed, that particular instance of the object teamleader can run its method teamleader->delete_team_leader().
<?php foreach($teamleaders as $teamleader) { ?>
<tr>
<td><?php echo $teamleader->first_name; ?></td>
<td><?php echo $teamleader->last_name; ?></td>
<td><img src="<?php echo '../img/' . $teamleader->photo;?>" alt="" class="img-fluid tiny-tl-img"></td>
<td><?php echo nl2br(substr($teamleader->bio, 0, 100));?>...</td>
<td><?php echo $teamleader->url_name;?></td>
<form action="index.php?a=edittl" method="post">
<td>
<input type="hidden" name="id" value="<?php echo $teamleader->id?>">
<input type="submit" class="btn btn-default" name="edit" value="Edit">
</td>
</form>
<form action="index.php" method="post">
<td>
<input type="hidden" value="<?php echo $teamleader->id?>" name="id">
<input type="submit" class="btn btn-danger" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete this team leader?')">
</td>
</form>
</tr>
<?php } ?>
In your case, the best solution I think you can use ajax/jQuery for editing or deleting action.
You don't need to use many forms as now.
The code in front-end like this:
<head>
<script src="jquery.min.js"></script>
</head>
...
<?php foreach($teamleaders as $teamleader) { ?>
...
<td><?php echo $teamleader->url_name;?></td>
<td>
<input type="submit" class="btn btn-default btn-edit" name="edit" value="Edit" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
<td>
<input type="submit" class="btn btn-danger btn-delete" name="delete" value="Delete" data-teamleaderid="<?php echo $teamleader->id?>">
</td>
</tr>
<?php } ?>
And the code with using ajax/jQuery is:
$(document).ready(function(){
$('.btn-delete').on('click',function(){
var _this = $(this);
var _teamleaderid = $(this).data('teamleaderid');
if (!confirm('Are you sure you want to delete this team leader?')) return false;
$.ajax(
url:'delete.php',
type:'post',
data: {
teamleaderid: _teamleaderid
},
dataType:'json',
success:function(data) {
if (data.return) {
$(_this).parent().parent().remove();/*Remove the current row in table html*/
alert('This teamleader has been deleted');
}
}
);
});
});
The delete.php code in backend is:
<?php
... #connect database
$teamleader_id = filter_input(INPUT_POST,'teamleader');
$check_delete = $dbconn->query("DELETE from teamleader_table WHERE id={$teamleader_id}");
print json_encode('return'=>$check_delete);
die();
?>

Oracle, php - add action in form

I'm trying to make this button checking if Employee ID passing via textbox exsits in Oracle Database and depend on result - show YES or NO after ':'.
But i have absolutely no idea how. I tried to make a form in a form:
<form action="addemp.php" method="POST">
<table>
<tr>
<td>Employee ID: </td>
<td><input type="text" name="empid" size=1/>
<form action="check.php" method="POST">
<input type="submit" name="check" value="Check?"> :
</form>
But no success since It can not be done.
Any suggestions?
EDIT:
check.php
<?php
$conn = oci_connect('hr', 'hr', 'hr');
$stid = oci_parse($conn, "select count(*) from employees where employee_id=TO_NUMBER(".$_GET['idprac'].")");
oci_execute($stid);
$result = oci_num_rows($stid);
// Use this $empId and check in query.
if($result==1){
echo "free";
} else
{
echo "owned";
}
?>
code in index.html
<td><input type="text" name="idprac" size=1/>
<input type="button" name="check" class='checkEmp' value="Check?"> : <span class='showResult'></span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.checkEmp').click(function(){
var empId= $('#idprac').val();
$.ajax({url:"check.php?idprac="+empId,cache:false,success:function(result){
$('.showResult').html(result);
}});
});
</script>
But ajax does not want to pass parameter to check.php (undefined error) and if i set var empID = whatever number gives me always 'owned'.
1) Nested <form> are NOT allowed.
2) To check employee exist or not. Use Ajax.
<form action="addemp.php" method="POST">
<table>
<tr>
<td>Employee ID: </td>
<td>
<input type="text" id='empId' name="empid" size=1/>
<input type="button" name="check" class='checkEmp' value="Check?"> : <span class='showResult'></span>
</td>
</tr>
</table>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.checkEmp').click(function(){
var empId= $('#empId').val();
$.ajax({url:"check.php?empId="+empId,cache:false,success:function(result){
$('.showResult').html(result);
}});
});
</script>
check.php
<?php
$empId = $_GET['empId'];
// Use this $empId and check in query.
if(employee id is available){
echo "Yes";
} else {
echo "No";
}
?>

ajax serialize gives different strings and form autosave doesnt work

I am trying to implement an autosave functionality using ajax and php. Here is my script:
function autosave() {
alert($("#myform").serialize());
$.ajax({
url: "form_creator.php",
data: $("#myform").serialize(),
type: 'POST',
success: function(data){
if(data && data == 'success') {
alert("saved");
// Save successfully completed, no need to do anything
}else{
alert(data);
// Save failed, report the error if you desire
// ....
}
}// end successful POST function
}); // end jQuery ajax call
// end setting up the autosave on every form on the page
}// end function autosave()
setInterval(autosave, 5000);
The problem is that I am getting different strings in the first alert and in the second one (in else section). In the first alert, the string seems to be correct with key=value&key2=value2.... but the second alert in else section gives me all html page. Is this correct behaviour? The autosave functionality doesn't work... Am I missing something?
<?php if((isset($_POST['sub'])) || (isset($_POST["autosave_check"])))
{ //save to db
}else{
?>
<form action="" method="post" id="myform" class="ajax_form" name="myform" enctype='multipart/form-data'>
<ul class="sorting dd-list" id="my_list">
//generate inputs
</ul>
<input type="hidden" name="chbox" class='hdbox' id="chbox" value="" />
<input type="hidden" name="save" value="<?php echo $idn?>" />
<input type="hidden" id="autosave_check" name="autosave_check" value="1">
<br class="clear" /> <br />
<div class="creator-buttons">
<h2 class="subheader-creator"> Akce </h2>
<table>
<tr>
<td>
<a href="javascript:;" class='radiusz addchbox' id="ch" value="Checkbox field"><?php echo $langa['formc'][10];?>
</a>
</td>
<td align='right'>
<input type="hidden" id="paths" value="<?php echo $patH?>" />
<input type="submit" name='sub' value='<?php echo $langa['formc'][13]?>' id="saveT" class='dugme1 submitforms' style="float:right;" />
</td>
</tr>
</table>
</div>
</form>
<?php } ?>
Its quite long code so just some parts.. Saving on button click works perfectly as for any other form.

Form in Form is not allowed, but what else

I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.

Categories