how to post select option value into jQuery ajax with name - php

I want to send section option value with ajax but only selection
and checkbox
the problem is that section and checkbox post undifined
thanx advanced
php code
<?php
$paramateres = new ParametresApp();
$paramateres->getparametres();
$max_pages_nbr = $paramateres->max_pages_nbr;
$idle_deconnexion = $paramateres->idle_deconnexion;
$show_chat = $paramateres->show_chat;
?>
html code
<form id="formparametres" class="form-row row shadow bg-white border row p-5 "
method="post">
<div class="form-check col-3 col-lg-3 ">
<input name="show_chat" id="show_chat" type="checkbox"
class="form-check-input" value="<?php echo $show_chat; ?>"
<?php if ($show_chat == 1) {
echo 'checked="true" ';
}
?>
<label class="form-check-label"
for="show_chat"> Autoriser messenger </label>
</div>
<div class="col-3 col-lg-3">
<label class="form-control-label">idle deconexion</label>
<input name="idle_deconnexion" class="form-control"
value="<?php echo $idle_deconnexion; ?>" required>
</div>
<div class="input-wrapper col-4 col-lg-4">
<label class="form-control-label">max_pages_nbr</label>
<select name="max_pages_nbr" class="form-control" required>
<?php echo '<option value="'.$max_pages_nbr.'">'.$max_pages_nbr.'</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>';?>
</select>
</div>
</form>
jquery code
$("form").submit(function (event) {
event.preventDefault();
var formData =
'show_chat=' + $('input[name=show_chat]').val() +
'&idle_deconnexion=' + $('input[name=idle_deconnexion]').val() +
'&max_pages_nbr=' + $('section[name=max_pages_nbr]').val() ;
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});

Try this
$("form").submit(function (event) {
event.preventDefault();
var formData = {'show_chat' : $('input[name=show_chat]').val(), 'idle_deconnexion' : $('input[name=idle_deconnexion]').val(), 'max_pages_nbr' : $('section[name=max_pages_nbr]').val() };
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
dataType: 'json',
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});
Hope this will help you.

i have solveur thanx for all
jquery
$(document).ready(function () {
$(':checkbox').change(function () {
if ($(this).attr("value") == 1) {
$(this).attr("value", 0)
} else {
$(this).attr("value", 1)
}
});
$("form").submit(function (event) {
event.preventDefault();
var formData =
'show_chat=' + $('#show_chat').attr("value") +
'&idle_deconnexion=' + $('input[name=idle_deconnexion]').val() +
'&max_pages_nbr=' + $('#max_pages_nbr option:selected').text() ;
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});
});
});
in may page src/admins/inc/save_parametres.inc.php
<?php
if (isset($_POST)){
$show_chat= $_POST['show_chat'];
$idle_deconnexion= $_POST['idle_deconnexion'];
$max_pages_nbr= $_POST['max_pages_nbr'];
$sql = " update dms_parametres set parm_value=".$show_chat ." where parm_name='show_chat';
update dms_parametres set parm_value='" . $idle_deconnexion . "' where parm_name='idle_deconnexion';
update dms_parametres set parm_value='" . $max_pages_nbr . "' where parm_name='max_pages_nbr';
";
if (!$parametres->connect()->multi_query($sql)) {
echo $parametres->connect()->error;
} else {
echo 'sucsess';
}
}
?>

Related

how to show select2 after select2 the data?

i just want to choose data again after select2 with select2, but not showing the data
loadUser();
function loadUser() {
$('#wg').empty();
$.ajax({
url: "<?php echo site_url('administrator/admin/getWg'); ?>",
type: 'GET',
dataType: 'JSON',
}).done(function(data) {
console.log('data: ', data);
$.each(data, function(i, item) {
$('#wg').append($('<option>', {
value: item.workgroup,
text: item.workgroup
}));
});
});
$('#wg').change(function() {
wg = $(this).val(),
$.ajax({
url: '<?php echo site_url("administrator/admin/getWg2"); ?>/' + wg,
dataType: 'JSON',
type: 'GET',
}).done(function(data) {
console.log('use: ', data);
$('#name').val(data.name);
});
});
}
but, if i use the input data without the select2 the data showing but only the first array, how to showing the all data ?
<div class="col-md-6">
<label for="wg">Workgroup</label>
<select class="form-control select2" id="wg" name="wg"></select>
</div>
<div class="col-md-6">
<label for="name">Name</label>
<input class="form-control select2" id="name" name="name">
</div>
just how to use the select2 twice.

How to select radio button and label with jquery

I have a problem with my radio button.
Since i styled my radio buttons, the function is not working as it should.
I've heard somewhere that i have to change something in my jquery, but i dont know what.
Can someone help me?
here is the Html code
<div class="form-group">
<div class="form-check">
<input class="form-check-input" name="state" data-order="<?php echo $obj->order_id; ?>" value="1" type="radio" id="gridCheck1"
<?php if($obj->status == '1'){ echo 'checked'; } else { ''; } ?> >
<label class="form-check-label" for="gridCheck1">
Koude producten
</label>
</div>
</div>
Script
$(document).ready(function(){
$('#gridCheck1,#gridCheck2,#gridCheck3').click(function() {
if(this.checked){
$.ajax({
type: "POST",
url: 'change_state.php',
data: {
state: $(this).attr('value'),
order_id: $(this).data('order'),
}, //--> send id of checked checkbox on other page
success: function(data) {
$('#container').html(data);
},
error: function() {
//alert('it broke');
},
complete: function() {
setInterval(function() {
location.reload();
}, 1000);
}
});
}
});

How to populate 2 selectbox from database using php and ajax

I have a problem, how to pass an argument with ajax in url: <?php echo base_url() . 'user_m/getAdministrator'; ?>" argument,
Because I tried to populate a selectbox based on another but I didn't have success.
html:
<div class="form-group">
<label>Denumirea intreprinderii:</label>
<select class="form-control marg-left-10" id="firm">
<?php if($all_firms): ?>
<?php foreach($all_firms as $firm): ?>
<option value="<?php echo $firm['id_firm']; ?>"><?php echo $firm['name_firm']; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<div class="form-group">
<label>Administrator</label>
<select class="form-control marg-left-10" id="admin">
</select>
</div>
php:
public function getAdministrator()
{
$id_firm = $_POST['id_firm'];
$this->load->database();
$sql = $this->db->query("SELECT * FROM users,firms WHERE firms.id_firm = users.fk_firm and users.id = ");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['name'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
return $this;
}
script:
$(document).ready(function()
{
$("#firm").change(function()
{
var id_firm=$(this).val();
var dataString = 'id_firm='+ id_firm;
$.ajax
({
type: "POST",
url: "<?php echo base_url() . 'user_m/getAdministrator'; ?>",
data: dataString,
cache: false,
success: function(html)
{
$("#admin").html(html);
}
});
});
});
Help me please.The first select box gets populated but the second doesn't.Please Help me.Exist a method to pass an argument to user_m/getAdministrator method? HELP ME PLEASE GUYS!!!!!
Php in js is not a good practice, try to write this on your main php file before calling the script
<script>
var base_url = "<?=base_url()?>";
</script>
and then in your js file:
$.ajax
({
type: "POST",
url: base_url+"user_m/getAdministrator",
data: dataString,
cache: false,
success: function(html)
{
$("#admin").html(html);
}
});
also, you can send data this way:
data: { id_firm: id_firm },

AJAX Function jQuery to PHP Access Response Variables

I am trying to send an AJAX function from jQuery to PHP. I can get it to the right page fine, but when I get there, how do I access the data from the AJAX call? It's probably something silly I'm missing.
jQuery:
$(document).ready(function() {
$("#assessform_page").on("submit", "#assessform", function(e) {
e.preventDefault();
var assessrows = {};
var url = $(this).attr("action");
$("div[data-test-num]").attr("data-test-num", function(index, value) {
assessrows[value] = {};
$(this).children("input[data-tab-row], select[data-tab-row]").attr("data-tab-row", function(i, v) {
assessrows[value][v] = $(this).val();
});
$(this).children("label[data-tab-row]").attr("data-tab-row", function(i, v) {
assessrows[value][v] = $(this).text();
});
});
$.ajax({
url: url,
data: assessrows,
type: "POST",
success: function() {
window.location.href = url;
},
});
});
});
HTML:
<form id="assessform" action="assessment.php" method="post" enctype="application/x-www-form-urlencoded" name="assessform">
<div data-test-num="1">
<label data-tab-row="objname">First Value: </label>
<select autofocus="autofocus" data-tab-row="status">
<option value="not_started">Not started</option>
<option value="in progress">In Progress</option>
<option value="obstacles">Obstacles</option>
<option value="excluded">Excluded</option>
<option value="done">Done</option>
</select>
<input type="text" data-tab-row="numer" value="" />
<input type="text" data-tab-row="denom" value="" />
<br />
<br />
</div>
<button type="submit" name="submit-button" id="submit-button">Submit</button><br />
<button type="button">Save</button><br />
</form>
PHP:
<?php ?> (I can't really do anything until I get this variable.)
What variable do I call to get the data from the AJAX, and what function, if any, do I need to call before I can do that?
EDIT:
If your assessment.php make a response as follows:
<?php
echo 'successfully_completed';
?>
The response data from PHP will be passed as follows:
$.ajax({
url: url, // assessment.php
data: assessrows,
type: "POST",
success: function(data) {
alert("The response is [" + data + "]");
// "The response is [successfully_completed]"
// If you want to pass this data to the next page
window.location.href = "/foo/success.php?data="+data;
}
});
$.ajax({
url: "url",
data: assessrows,
type: "POST",
success: function(data) {
console.log(data); //do something with data
window.location.href = url;
},
});

how to find a parent?

i have a problem with finding parent for form in this code:
<li class="comment<?php echo $comment[id]?>">
<div class="comment-content">
<div class="comment-top">
<div class="comment-nme">
<?php echo $comment[name]?>
</div>
<div class="comment-dt">
<?php echo $comment[dt]?>
</div>
</div>
<div class="comment">
<?php echo $comment[comment]?>
</div>
<a class="reply" href="#comment<?php echo $comment[id]?>">Ответить</a>
</div>
<div class="answer-form">
<form method="post" name="answer-form" class="ans">
<textarea class="comment-textarea" name="comment"></textarea>
<div class="a-comment-inputs">
<input type="hidden" name="parent_id" value="<?php echo $comment[id]?>">
<input type="hidden" name="status" value="new">
<div class="a-comment-name">
Имя</br>
<input type="name" name="name" class="a-comment-name">
</div>
<div class="a-comment-email" >
Eмейл</br>
<input type="email" class="a-comment-email" name="email">
</div>
</div>
<div class="comment-apply">
<button value="submit" onclick="return sendDataChild();" class="answer-but">Добавить</button>
</div>
</form>
</div>
<?php if($comment[childs]){ ?>
<ul class="commentsRoot<?php echo $comment[id]?>">
<?php echo commentsString($comment[childs]) ?>
</ul>
<?php } ?>
</li>
i use this jQuery function:
function sendDataChild() {
var form = $('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form[0].reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};
but it select every form that find on button click.
Can somebody advise how to solve it?
one possible solution
<button value="submit" onclick="return sendDataChild(this);" class="answer-but">Добавить</button>
then
//pass the clicked button reference then find the parent form of the button
function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form[0].reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};
Bind submit event on the form and pass the object to the form object to your method
$(document).ready(function(){
$("form[name='answer-form']").on('submit', function(){
sendDataChild($(this));
});
});
function sendDataChild(form) {
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form.reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};

Categories