i will get two jquery value in jquery variable how i will convert value in php variable
<script>
$(document).ready(function()
{
$(".slv").change(function()
{
var proid = $(this).attr('id');
var proval = $(this).val();
<?php $id1=?>proid;
<?php $id2=?>proval;
<?php
$data = array('name' => $id1,
'value'=>$id2,
'expire'=>'86500');
$this->input->set_cookie($data);
?>
});
});
Try set value into a cookie.
Here you will see how you can do it
how to assign javascript variable value to php variable
Use Ajax to post these values
$.ajax({
type: "POST",
url: "http://localhost/test/testcontroller/testfunction",
data: {'proid':proid,'proval':proval},
success: function(data)
{
},
}
});
And in controller you can get it like
$this->input->post('proid');
$this->input->post('proval');
Related
I am using CodeIgniter 4.1.3 for a project. I am facing issue caching value in Controller through Ajax post. Codes are following:
$(document).on('click','#showCont', function(){
var memID=$(this).attr('data-mem');
$data = {memID: memID};
//console.log($data);
$.ajax({
url: "<?=base_url();?>"+"/Member/getContact/",
headers: {'X-Requested-With': 'XMLHttpRequest'},
type: "POST",
data: $data,
cache: false,
success: function (cont) {
console.log(cont);
}
});
});
And here is the Controller:
<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Project_model;
use App\Models\General_model;
use CodeIgniter\I18n\Time;
class Member extends BaseController {
public function getContact(){
if ($this->request->isAJAX()) {
// echo "Hi"; printing Hi in console
//echo $query = $this->request->getPost('memID'); // returning nothing
print_r($_REQUEST);
}
}
And the route:
$routes->post('Member/getContact)', 'Member::getContact');
Below is the response of print_r in console.log
Array
(
[__tawkuuid] => e::xxxdomainxxx.com::FtBr5/am9LXZ8gvI7KxyHJZ279zLJEw11KZhdGh+4sflsIqrslML4R2NgRYqN8Vc::2
[__utmz] => 120264590.1625144765.12.5.utmcsr=l.facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/
[_ga] => GA1.2.2142405146.1621409499
[__utmc] => 120264590
[__utma] => 120264590.2142405146.1621409499.1629910859.1629978516.31
[_gid] => GA1.2.1913990981.1630182229
[ci_session] => e27a1e9ce4d0ee37f9b6569bd8eb15ab77fe45b2
)
Unable to understand why print_r $_INPUT is not showing sent data. Can anyone help me to solve this issue?
You are not writing the ajax correctly, you are going to have to pass the parameter in the url
$(document).on('click','#showCont', function(){
var memID=$(this).attr('data-mem');
$data = {memID: memID};
//console.log($data);
$.ajax({
url: "<?=base_url();?>"+"/Member/getContact/" + "<?=$data?>",
headers: {'X-Requested-With': 'XMLHttpRequest'},
type: "GET",
cache: false,
success: function (cont) {
console.log(cont);
}
});
});
On the other hand if you are using codeigniter 4 csrf validation, the method cannot be POST, you will have to use GET.
I want to insert some ajax post data into database. But when I'm clicking submit, no data is being inserted.
view(header.php)
$(function(){
$(".submit").click(function(){
transaction_student_id=$(".student_id").val();
transaction_particular_name=$(".particular_name").val();
transaction_id=$(".transaction_id").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url().'user/add_transaction'; ?>",
dataType: 'json',
data: {transaction_student_id: transaction_student_id,transaction_particular_name:transaction_particular_name,transaction_id:transaction_id},
success: function(data) {
}
});
});
});
Controller (User.php)
public function add_transaction()
{
$columns_and_fields = array('transaction_id','transaction_particular_name','transaction_student_id');
foreach ($columns_and_fields as $key)
$data[$key]=$this->input->post($key);
$query=$this->Mdl_data->insert_transaction($data);
if($query)
redirect('User','refresh');
}
Model (Mdl_data.php)
public function insert_transaction($data=array())
{
$tablename='transaction';
$query=$this->db->insert($tablename,$data);
return $query;
}
First of all, declare the variable in JavaScript with keyword var
var transaction_student_id=$(".student_id").val();
Before starting the Ajax use console.log() to know if the variables have data or not
The second thing is you are not getting the data with right way in the controller
Try like this
public function add_transaction()
{
$columns_and_fields = array('transaction_id' = $this->input->post('transaction_id'),
'transaction_particular_name' => $this->input->post('transaction_particular_name'),
'transaction_student_id' => $this->input->post('transaction_student_id'));
$query=$this->Mdl_data->insert_transaction($columns_and_fields);
if($query){
redirect('User','refresh');
}
}
Don't use the extra line of code without any reason
public function insert_transaction($data = array())
{
return $this->db->insert('transaction', $data);
}
Try debugging your code first.
Do you get all the data in controller? Try to dump POST values var_dump($_POST) in controller if ajax is successfully sending the data.
From there, you can see if the data in successfully sent from the front end.
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>user/add_transaction",
dataType: 'json',
data: {
transaction_student_id: transaction_student_id,
transaction_particular_name: transaction_particular_name,
transaction_id: transaction_id
},
success: function( data ) {
console.log( data );
},
error: function( xhr, status ) {
/** Open developer tools and go to the Console tab */
console.log( xhr );
}
});
change it
$(function(){
$(".submit").click(function(){
transaction_student_id=$(".student_id").val();
transaction_particular_name=$(".particular_name").val();
transaction_id=$(".transaction_id").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url().'user/add_transaction'; ?>",
dataType: 'json',
data: {transaction_student_id: transaction_student_id,transaction_particular_name:transaction_particular_name,transaction_id:transaction_id},
success: function(data) {
alert(data + ' id added' );
window.location.reload(); // force to reload page
}
});
});
});
at controller
public function add_transaction()
{ // use it only for ajax call or create another one
$columns_and_fields = array();
// 'transaction_id','transaction_particular_name','transaction_student_id'
foreach ($_POST as $key)
{
array_push($columns_and_fields , array(
'transaction_student_id' => $key['transaction_student_id'],
'transaction_particular_name'=>$key['transaction_particular_name'],
'transaction_id'=>$key['transaction_id']
)
);
}
$this->Mdl_data->insert_transaction_array($columns_and_fields);
}
and at model create new method
public function insert_transaction_array($data=array())
{
$tablename='transaction';
$this->db->insert_batch($tablename,$data);
}
i want to filtering data on the table using select box. When user choose value from select box then column on the table will filtered. I mean the table just show data that contain value on select box that user choose. Can anyone help me
this is my ajax :
<script>
$("#inputJenis").on('change',function(){
if($("#inputJenis").val() != 'Ganjil'){
$.ajax({
type: "POST",
url:'<?php echo base_url()?>search/filter',
data:'selectvalue='+$('#inputJenis').val(),
cache: false,
success: function(data){
$(#tableData).html(data);
},
error: function(data){
//return false;
}
});
});
});
</script>
this is my controller :
public function filter()
{
$this->load->helper('url');
$this->load->model('filter_model');
$this->filter_model->getData();
$data = $this->filter_model->getData();
echo json_encode($data);
}
this is my model :
public function getData($type)
{
$this->db->select('jenis');
$query = $this->db->get('tahunajaran');
return $query->result();
echo json_encode($query);
}
This code didn't work. help me please
Try this:
AJAX:
<script type="text/javascript">
var base_url='<?php echo base_url(); ?>';
</script>
<script>
$("#inputJenis").change(function(e){
if($("#inputJenis").val() != 'Ganjil'){
$.ajax({
type: "POST",
url:base_url+'search/filter',
data:'selectvalue='+$('#inputJenis').val(),
cache: false,
success: function(data){
$(#tableData).html(data);
},
error: function(data){
//return false;
}
});
});
});
</script>
Controller
public function filter()
{
$this->load->helper('url');
$this->load->model('filter_model');
$this->filter_model->getData();
$selectValue=$this->input->post('selectvalue')
$data = $this->filter_model->getData($selectValue);
echo json_encode($data);
}
Model
public function getData($selectValue)
{
$this->db->select('jenis');
//Change it with your Field Name
$query = $this->db->where('SomeField',$selectValue)->get('tahunajaran');
return $query->result_array();
}
I have searched a lot of tutorials with POST methods and saw answered questions here too but my POST still doesn't work...I thought i should post it here if you guys see something that i don't!
My js - messages.js:
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
return false;
});
});
My view - chat.php:
<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid
<form method="post">
<input id="textbox" type="text" name="textbox">
<input id="send" type="submit" name="send" value="Send">
</form>
Last My controller - chat.php
//more functions here
function post_action()
{
if($_POST['textbox'] == "")
{
$message = "You can't send empty text";
}
else
{
$message = $_POST['textbox'];
}
echo $message;
}
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
});// you have missed this bracket
return false;
});
});
In codeigniter there is no need to sennd "data" in ajax post method..
here is the example .
searchThis = 'This text will be search';
$.ajax({
type: "POST",
url: "<?php echo site_url();?>/software/search/"+searchThis,
dataType: "html",
success:function(data){
alert(data);
},
});
Note : in url , software is the controller name , search is the function name and searchThis is the variable that i m sending.
Here is the controller.
public function search(){
$search = $this->uri->segment(3);
echo '<p>'.$search.'</p>';
}
I hope you can get idea for your work .
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserController extends CI_Controller {
public function verifyUser() {
$userName = $_POST['userName'];
$userPassword = $_POST['userPassword'];
$status = array("STATUS"=>"false");
if($userName=='admin' && $userPassword=='admin'){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
}
}
function makeAjaxCall(){
$.ajax({
type: "post",
url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser",
cache: false,
data: $('#userForm').serialize(),
success: function(json){
try{
var obj = jQuery.parseJSON(json);
alert( obj['STATUS']);
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
The question has already been answered but I thought I would also let you know that rather than using the native PHP $_POST I reccomend you use the CodeIgniter input class so your controller code would be
function post_action()
{
if($this->input->post('textbox') == "")
{
$message = "You can't send empty text";
}
else
{
$message = $this->input->post('textbox');
}
echo $message;
}
<script>
$("#editTest23").click(function () {
var test_date = $(this).data('id');
// alert(status_id);
$.ajax({
type: "POST",
url: base_url+"Doctor/getTestData",
data: {
test_data: test_date,
},
dataType: "text",
success: function (data) {
$('#prepend_here_test1').html(data);
}
});
// you have missed this bracket
return false;
});
</script>
I think this is a simple one.
I have a Codeigniter function which takes the inputs from a form and inserts them into a database. I want to Ajaxify the process. At the moment the first line of the function gets the id field from the form - I need to change this to get the id field from the Ajax post (which references a hidden field in the form containing the necessary value) instead. How do I do this please?
My Codeigniter Controller function
function add()
{
$product = $this->products_model->get($this->input->post('id'));
$insert = array(
'id' => $this->input->post('id'),
'qty' => 1,
'price' => $product->price,
'size' => $product->size,
'name' => $product->name
);
$this->cart->insert($insert);
redirect('home');
}
And the jQuery Ajax function
$("#form").submit(function(){
var dataString = $("input#id")
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "/home/add",
data: dataString,
success: function() {
}
});
return false;
});
As always, many thanks in advance.
$("#form").submit(function(){
var dataString = $("input#id")
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "/home/add",
data: {id: $("input#id").val()},
success: function() {
}
});
return false;
});
Notice data option in the ajax method. Now you could use $this->input->post('id') like you are doing in the controller method.
Why not slam it to the end of
url: "/home/add",
like
url: "/home/add/" + $("input#id").val(),
Then I guess codeigniter will treat it like a normal parameter ... ?