I have a form that includes a dropdown box. This dropdown's options are filled dynamically, using PHP, from my database. When my form is submitted, the row containing the ticketId selected in the dropdown is removed from the database. What I would like is for, after the AJAX call is successful, have this list repopulate without the row that was just removed.
my form:
<form name="transferTicketForm" id="transferTicketForm">
<div class='form-group'>
<label for="ticket_number">Choose ticket:</label>
<select name="ticket_number" id="ticket_number" class="form-control">
<?php foreach ($assigned_tickets as $ticket): ?>
<option value="<?php echo($ticket->ticketId); ?>"><?php echo ($ticket->ticketId . " - " . $ticket->headline); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="assignTo">Transfer to:</label>
<select name="assignTo" id="assignedTo" class="form-control">
<?php foreach($employees as $employee): ?>
<option value='<?php echo($employee->userId); ?>'><?php echo ($employee->firstName . " " . $employee->lastName);?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="transferComment">Comment:</label>
<textarea name="transferComment" id="transferComment" class="form-control"></textarea>
</div>
<div class="text-center">
<input class="btn btn-primary" type="button" id="doTransferTicketButton" value="Transfer Ticket">
</div>
</form>
my Ajax call:
$('#doTransferTicketButton').click(function(){
$.ajax({
type:'POST',
url:'<?php echo site_url();?>ticket_system/transfer_and_comment_ticket',
data:{
'assigned': $('#assignedTo').val(),
'comment': $('#transferComment').val(),
'ticketId': $('#ticket_number').val()
},
success: function(data){
$target_ticket = $('#ticket_number').val();
$('#ticket_' + $target_ticket).remove();
$('#assigned').empty();
$('#transferTicketOptions').slideToggle();
}
});
});
Things I have tried:
success: function(data){
$target_ticket = $('#ticket_number').val();
$('#ticket_' + $target_ticket).remove();
$('#ticket_number').empty();
$('#transferTicketOptions').slideToggle();
}
and
success: function(data){
$target_ticket = $('#ticket_number').val();
$('#ticket_' + $target_ticket).remove();
$('#ticket_number')[0].reset();
$('#transferTicketOptions').slideToggle();
}
Neither of which work for what I'm trying to do.
Any help would be greatly appreciated. Thank you!
First make your AJAX-file echo the ticket ID when removed. This ID will be passed to the success function. And on successs run:
success: function(data){
$("#ticket_number option[value=data]").remove();
}
The code is not testet, but i think it would work.
Related
I'm facing scroll-linked trouble while making chained dropdown.
I'm using php as programming language and codeigniter as the framework, here are the codes
View Codes
<div class="form-row">
<div class="col-md-4">
<label for="inputBody">Body Number</label>
<select name="inputBody" id="inputBody" class="form-control" required="required">
<?php foreach ($body as $bd) { ?>
<option value="<?php echo $bd->bodynumkids ?>" onchange="ambildata('+<?php echo $bd->bodynumkids ?>+');"><?php echo $bd->bodynumkids ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-6">
<label for="inputKiddie">Kiddies Name</label>
<input type="text" name="inputKiddie" id="inputKiddie" class="form-control" placeholder="ex. Super Cop" required="required">
</div>
<script type="text/javascript">
function ambildata(x) {
$.ajax({
type:'POST',
data :'input='+x,
url :'<?php echo base_url()."Repairpaint/chained" ?>',
dataType: 'json',
success: function(data){
console.log(data);
}
})
}
</script>
</div>
Controller Codes
public function chained()
{
$dataKiddie = $this->input->post('input');
$where = array('bodynumkids'=> $dataKiddie);
$namakiddie = $this->Model_repairpaint->chaincb('kiddiejadi', $where)->result();
echo json_encode($namakiddie);
}
The result wasn't appear in console, only these warning that appear
This site appears to use a scroll-linked positioning effect. This may not work well with asynchronous panning; see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects for further details and to join the discussion on related tools and features!
What expected is the data from database appear in console when I click the dropdown. Can anyone here tell me the solution? Thank you before, for helping me.
Put ambildata()function in <select> tag not in <option>
<select name="inputBody" id="inputBody" class="form-control" required="required" onchange="ambildata(this.value)">
And the second thing some changing in the Ajax call
url : '<?php echo base_url("Repairpaint/chained"); ?>'
OR
url : '<?php echo base_url(); ?>' + 'Repairpaint/chained';
Sending Data
data : { 'input': x }
I am using CodeIgniter. I have a view button on the view page. If someone clicks on it then a custom popup will open and It will ask for feedback and approve. Also, I set input type hidden in this popup. After filling the form user will click on submit button.
But I am not getting the value in the controller but I am getting the hidden input value.
View
<?php echo form_open('Employee_control/admin_action_leave','id="admin_action_leave"'); ?>
View
<div class="view_popup_profile" id="popup-2" style="display: none;">
<div class="opacity"></div>
<div class="profile_content">
<div class="profile_header clearfix">
<div class="profile_header_right">
<input type="hidden" name="roster_id_send" id="roster_id_send" value="2">
<select name="admin_approved_status" id="admin_approved_status">
<option value="" disabled selected>Status</option>
<option value="1">Approved</option>
<option value="0">Pending</option>
<option value="-1">Reject</option>
</select>
<?php echo form_error('admin_approved_status'); ?>
</div>
</div>
<div class="profile_body">
<div class="row">
<div class="col-md-6 text_area_box">
<div class="admin_feedback">
<h3>Feedback</h3>
<textarea class="form_control" name="admin_feedback" id="admin_feedback"></textarea>
<?php echo form_error('admin_feedback'); ?>
<button type="submit" class="btn">Submit</button>
</div>
</div>
</div>
</div>
<div class="profile_footer">
Close
</div>
</div>
</div>
<?php echo form_close(); ?>
Controller
public function admin_feedback_leave(){
echo $admin_approved_status=$this->input->post('admin_approved_status');
echo $admin_feedback=$this->input->post('admin_feedback');
echo $roster_id_send=$this->input->post('roster_id_send');
}
I tried using ajax instated of form
Same issue I am getting. It's display only hiddle value
$("#admin_action_leave").validate({
// Specify the validation rules
rules: {
admin_approved_status:{
required:true
},
admin_feedback:{
required:true,
minlength:15,
maxlength:250
}
},
submitHandler: function(form) {
//form.submit();
//var a= document.getElementById("admin_approved_status").value;
//alert(a);
var admin_approved_status = $('#admin_approved_status').val();
var admin_feedback = $('#admin_feedback').val();
var roster_id_send = $('#roster_id_send').val();
// alert(admin_feedback);
//alert(roster_id_send);
//alert(admin_approved_status);
$.ajax({
url: baseUrl + "/Employee_control/admin_feedback_leave",
method: "POST",
data: {
admin_approved_status: admin_approved_status,
admin_feedback:admin_feedback,
roster_id_send:roster_id_send
},
success: function(data)
{
alert(data);
}
}); //AJAX
}
});
After implementing the code. I am just sharing the small part of the code.
My issue is I am not able to send the data to the controller. I tried two-way one using ajax and second using form submit. I am sharing the form submit code.
When I am adding the below code then it's not sending the data to the controller. If I remove the if and foreach condition then I am getting the output.
<?php $n=1; if(isset($admin_data_leave)){foreach ($admin_data_leave as $row) {?>
<!--some html code here-->
<?php $n++;}}else{echo "No data found";}?>
Whole code
<?php echo form_open('Employee_control/admin_feedback_leave', 'id="admin_action_leave"'); ?>
<table cellspacing="0" class="applied_leave_list_table">
<tbody>
<?php
$n = 1;
if (isset($admin_data_leave)) {
foreach ($admin_data_leave as $row) {
?>
<tr>
<td><?php echo $n; ?></td>
<td> View </td>
</tr>
<div class="view_popup_profile" id="popup-<?= $row->roster_id; ?>" style="display: none;">
<div class="profile_content">
<div class="profile_header_right">
<input type="hidden" name="roster_id_send" id="roster_id_send" value="<?php echo $row->roster_id; ?>">
</div>
</div>
<h3>Admin Feedback</h3>
<textarea class="form_control" name="admin_feedback" id="admin_feedback"></textarea>
<?php echo form_error('admin_feedback'); ?>
<button type="submit" class="btn">Submit</button>
</div>
<?php
$n++;
}
} else {
echo "No data found";
}
?>
</tbody>
</table>
<?php echo form_close(); ?>
Jquery validation
$('#admin_action_leave').each( function(){
var form = $(this);
form.validate({
rules: {
admin_approved_status:{
required:true
},
admin_feedback:{
required:true,
minlength:15,
maxlength:250
}
}
});
});
When I am adding the below code then it's not sending the data to the
controller. If I remove the if and foreach condition then I am getting
the output.
...which means, the content of the variable $admin_data_leave is not fulfilling the condition, ie. Either it is not set(not defined) or its not a traversible object or its empty.
Try putting the following code before the condition and look at the output...
echo "<pre>";
var_dump($_POST);
echo "</pre>";
die();
I am trying to implement dependent dropdown using ajax and php. But anyhow the response from second dropdown is not coming. after checking in console, I am able to see the id of first dropdown but no response from second dropdown. So, When i click on first dropdown, I just get a blank value in Second dropdown.
HTML Code
<div class="col-md-6">
<?php
require_once('db.php');
$varient_result = $conn->query('select * from tv_varient');
?>
<select name="varient" id="varient-list" class="form-control c-square c-theme" required>
<option value="">Select Varient</option>
<?php
if ($varient_result->num_rows > 0) {
// output data of each row
while($row = $varient_result->fetch_assoc()) {
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}
}
?>
</select>
</div>
</br></br>
<label for="inputPassword3" class="col-md-4 control-label" style="margin-left: 15px;">Price:</label>
<div class="col-md-6">
<select name="price" id="price-list" class="form-control c-square c-theme" required>
<option value=''>Select Price *</option>
</select>
<div>
</div>
</div>
</div>
</div>
</div>
AJAX Code
<script>
$('#varient-list').on('change', function(){
var varient_id = this.value;
$.ajax({
type: "POST",
url: "get_price.php",
data:'varient_id='+varient_id,
success: function(result){
$("#price-list").html(result);
}
});
});
</script>
get_price.php
?php
require_once('db.php');
//$country_id = mysqli_real_escape_string($_POST['country_id']);
//var_dump($country_id);
//var_dump($_POST['country_id']);
$varient_id = $_POST['veriant_id'];
echo $varient_id;
//var_dump($varient_id);
var_dump($_POST['varient_id']);
if($varient_id!='')
{
$states_result = $conn->query('select * from tv_price where veriant_id='.$varient_id.'');
$options = "<option value=''>Select Price</option>";
while($row = $states_result->fetch_assoc()) {
$options .= "<option value='".$row['price']."'>".$row['price']."</option>";
}
echo $options;
}
?>
Try below code,
$(document).on('change', '#varient-list', function(e)
{
var varient_id = this.value;
$.ajax({
type: "POST",
url: "get_price.php",
data:'varient_id='+varient_id,
success: function(result){
$("#price-list").html(result);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
varient_id & veriant_id on the POST is wrong (the e & a difference).
$varient_id = $_POST['veriant_id']; should be $varient_id = $_POST['varient_id'];
I have the following HTML
<div class="question-set">
<div class="row question">
<div class="col-md-2">
<select class="form-control" name="type[]">
<option value="Teacher feedback">Teacher feedback</option>
<option value="Genral Question">Genral Question</option>
<option value="Informative Question">Informative Question</option>
</select>
</div>
<div class="col-md-8">
<input type="text" class="form-control" name="questions[]" placeholder="Enter the question here" />
</div>
</div>
<div class="row question">
<div class="col-md-2">
<select class="form-control" name="type[]">
<option value="Teacher feedback">Teacher feedback</option>
<option value="Genral Question">Genral Question</option>
<option value="Informative Question">Informative Question</option>
</select>
</div>
<div class="col-md-8">
<input type="text" class="form-control" name="questions[]" placeholder="Enter the question here" />
</div>
</div>
</div>
In short there is a select tag with name type[] and a text input field names questions[]. I need to pass the values to a php page as an ajax call to insert into the database. I have the following AJAX code..
$.ajax({
method: "POST",
url: "feedback_create_form_process.php",
data: {"questions": $("input[name='questions[]']").val(), "type": $("select option:selected").val()},
success: function(data){
alert(data);
}
});
Here is my PHP code
<?php
include 'con.php';
if (isset($_POST) && sizeof($_POST)) {
$question = $_POST['questions'];
$type = $_POST['type'];
echo count($question);
foreach( $question as $key => $n ) {
$result = $con->query("insert into form question(null, ".$question[$key].", ".$n.")");
}
}
?>
The count is returned as 1. It sends only the first question and not the array. I want the values in an array so i can do an insert in a loop.
As Documented in JQuery .Val()
Description: Get the current value of the first element in the set of matched elements.
You can use
var values = $("input[name='questions[]']").map(function(){return $(this).val();}).get();
or
var values = [];
$("input[name='questions[]']").each(function() {
values.push($(this).val());
});
You can make use of jQuery's map() function to return the values as an array, like so:
var questions = $("input[name='questions[]']").map(function() {return $(this).val()}).get();
var types = $("select[name='type[]']").map(function() {return $(this).val()}).get();
$.ajax({
method: "POST",
url: "feedback_create_form_process.php",
data: {"questions": questions, "type": types},
success: function(data){
alert(data);
}
});
EDIT: Realizing a similar solution was provided by ABDU_GO. I find my script to be a bit more readable, however if you find this to be your solution, I'd suggest accepting his answer.
I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal.
I'm using Magento CE 1.7.0.2
<div id="display">
<?php
if (sizeof($final_na)>0) {
$i = 0;
?>
<select size="5" class="auto-search" style="height:132px;width: 420px;padding:0px;" name="hero">
<?php foreach ($final_na as $key => $value) { ?>
<option style="text-align:center;padding: 5px 0 0;" value="<?php echo $final_na1[$i]; ?>" class="display_box" align="left" onclick="fill('<?php echo $value;?>')">
<?php echo $value; ?>
</option>
<?php $i++; ?>
<?php } ?>
</select>
<?php } ?>
</div>
<label for="description" class="rightgap"><?php echo Mage::helper('marketplacepartner')->__('Description') ?> :</label>
<textarea name="description" class="required-entry " id="description" rows="5" cols="75" ><?php echo $description?></textarea>
<label for="price" class="rightgap"><?php echo Mage::helper('marketplacepartner')->__('Price') ?> :</label>
<input type="price" class=" required-entry widthinput" name="price" id="price" value="<?php echo $price?>"/>
here i'm displaying a dropdown,based on my selection from this dropdown have to fill the remaining fields have to fill.
for example selected option value is 25 this is product id in my site based on that id values have to fill below like description,price....
for this i have to get that option value in one variable...
Using this script i'm getting the selected option value
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#display').click(function(){
var searchbox = $("#display :selected").val();
alert(searchbox);
});
});
</script>
but i can't store it in a variable in php
Any ideas ?
you can send the value of searchbox via json to targetfile.php
inside your click handler
$.ajax({
url: "targetfile.php",
type: "POST",
dataType : "json",
data: {"hero":searchbox},
success: function(data) {
// something that should happen after success
// data is filled with the output of targetfile.php (should be json)
}
});
do print_r($_POST); in targetfile.php to see what you get.
And take a look at the Console tab of firebug.
http://api.jquery.com/jQuery.ajax/