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 },
Related
I made 2 dependent dropdown in the same page.
One of them is inside foreach statement and it doesn't work at all. Meanwhile the other one works well. The name of id are totally different. Both of dependent dropdown lists refer to the same function in controller. I thought its the main problem and tried to make a different function, but it didn't change anything.
VIEW :
<tr>
<td><label>Category</label></td>
<td>
<div class="form-group">
<select id="edit_category" class="form-control" name="edit_category">
<option value="">Select Category</option>
<?php foreach ($categories as $cat) : ?>
<option <?php ?> value="<?php echo $cat->id; ?>"><?php echo $cat->name
</option>
<?php endforeach; ?>
</select>
</div>
</td>
</tr>
<tr>
<td><label>Product</label></td>
<td>
<div class="form-group">
<select name="edit_product" id="edit_product" class="form-control" style="width:350px">
<option value="">Select Product</option>
</select>
</div>
</td>
</tr>
SCRIPT :
<script type="text/javascript">
$(document).ready(function() {
//DEPENDENT DROPDOWN - ADD ITEM :
$('#add_category').on('change', function() {
$('#add_product').html('<option value="">Select Product</option>');
var catID = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/item/dependentDL') ?>",
method: "POST",
data: {
id_p_category: catID
},
async: true,
dataType: "json",
success: function(data) {
$('#add_product').html(data);
},
error: function(error) {
alert(error);
}
});
return false;
}); //END - DROPDOWN - ADD ITEM
//DEPENDENT DROPDOWN - EDIT ITEM :
$('#edit_category').on('change', function() {
$('#edit_product').html('<option value="">Select Product</option>');
var edit_catID = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/item/dependentDL') ?>",
method: "POST",
data: {
id_p_category: edit_catID
},
async: true,
dataType: "json",
success: function(data) {
$('#edit_product').html(data);
},
error: function(error) {
alert(error);
}
});
return false;
});
});
</script>
You should try with class instead of id because id is unique and your dropdown in the foreach loop so please try with class like below
$('.add_category').on('change', function() {
$('#add_product').html('<option value="">Select Product</option>');
var catID = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/item/dependentDL') ?>",
method: "POST",
data: {
id_p_category: catID
},
async: true,
dataType: "json",
success: function(data) {
$('#add_product').html(data);
},
error: function(error) {
alert(error);
}
});
return false;
}); //END - DROPDOWN - ADD ITEM
//DEPENDENT DROPDOWN - EDIT ITEM :
$('.edit_category').on('change', function() {
$('#edit_product').html('<option value="">Select Product</option>');
var edit_catID = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/item/dependentDL') ?>",
method: "POST",
data: {
id_p_category: edit_catID
},
async: true,
dataType: "json",
success: function(data) {
$('#edit_product').html(data);
},
error: function(error) {
alert(error);
}
});
return false;
});
This is a code with DropDown list which can update the database upon on change, it doesn't work for some reason. I realize that the value $ gp_name didn't send out, can someone help me to fix it?
<select name='status' id='status'>
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Inquiry">Inquiry</option>
</select>
<input type="hidden" name="gp_name" id="gp_name" value="<?php echo $gp_name;?>" />
<div id="autosavenotify"></div>
<script>
$(document).ready(function() {
var gp_name = $('#gp_name').val();
$('select').on('change', function() {
var statusVal = $(this).val();
var gp_name = $('#gp_name').val();
alert(statusVal,gp_name);
$.ajax({
type: "POST",
url: "save.php",
data: {
statusType: statusVal,
gp_name: gp_name
},
success: function(msg) {
$('#autosavenotify').text(msg);
}
})
});
});
</script>
save.php
<?php
require_once("connMysql.php");
$gp_name=$_POST['gp_name'];
$st=$_POST['statusType'];
$qry ="UPDATE lotus_gp SET status='".$st."' where gp_name='".$gp_name."'";
$done = mysql_query($qry);
if($done)
{
echo $gp_name;
echo $st;
}
?>
First, check if the data is getting from your front-end sides on your PHP page.
if(isset($_POST['gp_name']) && isset($_POST['statusType'])){
$gpname = $_POST['gp_name']; // echo it to ensure
$satype = $_POST['statusType']; // echo it to ensure
}
If the data is not printed then you might have an issue with inputs.
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';
}
}
?>
I am warking in phalcon framework, and i am trying to call controller's method to get simple string using jquery-ajax. When i placed my ajax call inside $(document).ready(function() ajax call worked, but when i placed same code inside $('#dugme').click(function() ajax call reported error. I am confused. Here is my view code:
<script type="text/javascript">
$(document).ready(function(){
//alert($("#nesto").val());
//atr = $(".klasa").attr("id");
//alert("Id je: " + atr)
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
$('#dugme').click(function(){
$.ajax({
url: '<?php echo $this->url->get("xml/posalji");?>',
type: 'POST',
dataType: 'json',
success: function(data){
alert(data);
},
error: function(){
alert("Neuspjesan JSON zahtjev!");
}
});
}) ;
});
</script>
<h2>Basic example</h2>
<?php echo Tag::form("xml/pretraga"); ?>
<p>
<label for="name">Title</label>
<?php $opt = array('title', 'id'=>'nesto', 'size'=>'10');
$buttopt = array('Show', 'id'=>'dugme','class'=>'klasa');
?>
<?php echo Tag::textField($opt) ?>
</p>
<p>
<?php echo Tag::SubmitButton($buttopt) ?>
</p>
</form>
and here is my action code:
public function posaljiAction(){
$this->view->disable();
$data = "My name is Nedimo";
echo json_encode($data);
}
Please, can anyone tell me what is wrong in my code.
I'm having trouble getting ajax to work with jquery and codeigniter. I've followed multiple tutorials but nothing seems to work. I'm trying to implement an upvote button that sends post data using ajax to be inserted into a db. Any help is much appreciated.
The code from my view:
<pre>
<?php foreach($query->result() as $row): ?>
<div>
<form action="" method="post">
<input type="hidden" name="story_id" value=<?php echo $row->id; ?>>
<input type="submit" name="story_id" class="button" id="submit_btn" value=<?php echo $row->id;?>>
</form>
</div>
<?php endforeach; ?>
</pre>
The jquery script I'm using:
<pre>
<script type="text/javascript">
$(document).ready(function(){
$(".button").click(function(){
var form_data = {
story_id: $(this).val()
};
$.ajax({
url: "<?php echo base_url('cyoa/upvote'); ?>",
type: 'POST',
data: form_data,
success: function()
{
$("#upvote").hide();
}
});
return false;
});
});
</script>
</pre>
Best way is to serialize your form data and send that to the server:
<script type="text/javascript">
$(document).ready(function(){
$(".button").click(function(){
var form_data = $("#myform").serialize();
$.ajax({
url: "<?php echo base_url('cyoa/upvote'); ?>",
type: 'POST',
data: form_data,
success: function()
{
$("#upvote").hide();
}
});
return false;
});
});
</script>
To access the form, give it a id:
<form id="myform" action="" method="post">
I strongly suggest to look at this plugin (and use it) :
JQuery Form Plugin
You can use form serialization to pass on data to php as below
First Give ID "storyform" to your form tag
$.ajax({
url: "<?php echo base_url('cyoa/upvote'); ?>",
type: 'POST',
data: $("#storyform").serialize(),
success: function()
{
$("#upvote").hide();
}
});
try using json objects,
view....
<pre>
<?php foreach($query->result() as $row): ?>
<div>
<form action=" controller_name/upvote" method="post">
<input type="hidden" name="story_id" id = "story_id" value=<?php echo $row->id; ?>>
<input type="submit" name="story_id" class="button" id="submit_btn" value=<?php echo $row->id;?>>
</form>
</div>
<?php endforeach; ?>
</pre>
Then your controller,
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controller_name extends CI_Controller {
function __construct()
{
parent::__construct();
//loading libraries
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
}
//##############################################################################
function upvote(){
//setting validation rules
$this->form_validation->set_rules('story_id', 'story Id', 'required|xss_clean');
if ($this->form_validation->run() == true){
//if validations ok, send the data to database
$this->load->model('your_model');
$params = ($this->input->post('story_id');
$query = 'INSERT INTO table_name (story_id) VALUES(?)';
$result = $this->your_model_name->method_name($query,$params);
echo '{"validation_result": "passed"}';
}
else{
$output = '{"story_id":"'.form_error('story_id').'"}';
echo $output;
}
}
}
?>
Then Your java script file
$(document).ready(function(){
$('#submit_btn').live('click',function(){
var form_data = {
$story_id: $('#story_id').val()
};
$.ajax({
type: "POST",
url: baseurl+"controller_name/upvote",
dataType: "json",
data : form_data,
success:
function(data){
if(data.validation_result == "passed"){
$("#upvote").hide();
}
else{
//form_validation errors
}
}
});
return false;
});
});