pass value from view to controller in codeigniter using onclick - php

I want to send the id of a button onclick frome view to controller through ajax but it return the error:
500 Internal Server Error
my view:
<a data-toggle="modal"data-target="#Add_Money_to_campaign" ><button onclick="addId('<?php echo $id;?>');"> Add</button></a>
controller
$id= $this->input->post('dataString');
$this->model->function($id);
script:
function addId(id){
var dataString = id
alert(dataString)
$.ajax({
type: "post",
url: "<?php echo base_url('controller/function');?>",
data: {dataString:dataString},
cache: false,
success: function(html){
alert(html);
}
});
}

I'm updating your code
Your view:
<a data-toggle="modal" data-target="#Add_Money_to_campaign" ><input type="submit" value="Add" onclick="addId(<?php echo $id;?>);" /></a>
Your JS:
function addId(id){
var id = id
alert(id);
$.ajax({
type: "post",
url: "<?php echo base_url('controller/function');?>",
data: {id:id},
cache: false,
async: false,
success: function(result){
console.log(result);
}
});
}
Your function
$id= $this->input->post('id');
echo $id;
$this->model->function($id);// you might have error over here you need to specify your model_name as my_model

Related

PHP AJAX Getting undefined index id ,tried many ways couldnt solve it

I'm making an click event in which I send the id of the employee. I can see the value using console.log(); the employee id is getting shown, but I keep getting an index :id error.
<input type="button" class="btn btn-primary" value="Appointment Letter" onclick="downloadempappointmentletter('<?php echo $data->employee_id;?>')">
function downloadempappointmentletter(id) {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: id },
success: function (data) {
console.log(data);
}
});
}
public function downloadappointmentletter()
{
$empid = $_POST['id'];
}
I keep getting error:
undefined index :id
However when I echo $data->employee_id; it shows 1 etc. What am I missing?
You just comment $empid = $_POST['id']; this line and var_dump($_POST); it will shows all $_POST values from ajax
look through browser Network tab ,check ajax post id or not after that we can follow up the problem
try this
<input type="button" class="btn btn-primary" value="Appointment Letter" onclick="downloadempappointmentletter('<= $data->employee_id ?>')">
<input type="button" class="mybutton btn btn-primary" value="Appointment Letter"
data-value="<?php echo $data->employee_id; ?>">
now
$(document).ready(function() {
$(".mybutton ").click(function(){
let id = $(this).attr('data-value');
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: id },
success: function (data) {
console.log(data);
}
});
});
});
function downloadempappointmentletter(id) {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { "id": id },
success: function (data) {
console.log(data);
}
});
}
Try this. I think you forgot ("") when sending data
I fixed it instead of an onclick event ,i made it work through calling it in <a href="">
function downloadempappointmentletter(id) {
var ID = id;
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Admin/Employee/downloadappointmentletter",
data: { id: ID },
success: function (data) {
console.log(data);
}
});
}

CodeIgniter: jQuery post and load next page

I am successfully able to save the input value using ajax post but it is not loading the next page.
Client Side code:
<script type="text/javascript">
function notifyEmail() {
var form_data = {
'email':$('#inviteEmail').val()
};
alert($('#inviteEmail').val());
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});
}
</script>
<input type='text' id='inviteEmail' placeholder='Enter email'/>
<a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a>
Controller Code:
function email_invites()
{
$this->load->model('emailInvites');
if($query = $this->emailInvites->saveEmailInvite())
{
$this->load->view('emailInvites');
}
}
What I do is the following which could help you
<script type="text/javascript">
function notifyEmail() {
var form_data = {
'email':$('#inviteEmail').val()
};
alert($('#inviteEmail').val());
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
dataType:'json',
success: function(msg) {
if(msg.response)
//load the content in the body or wherever you want
jQuery('body').load("<?php echo site_url('main/email_invites/show'); ?>");
else
console.log("Opps, Something wrong happend");
return true;
}
});
}
</script>
<input type='text' id='inviteEmail' placeholder='Enter email'/>
<a href='#' name='email' id='invite-all' onclick='notifyEmail();' class='btn'>Notify Me</a>
In the controller
function email_invites($action = 'process')
{
if($action == 'process')
{
$this->load->model('emailInvites');
$query['response'] = $this->emailInvites->saveEmailInvite();
echo json_encode($query);
}
else
{
$this->load->view('emailInvites');
}
}
You do not need to echo the url, just use it in ajax call like this,
$.ajax({
url: 'main/email_invites',
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});
If site_url method returns the required url, then the ajax call needs to be,
$.ajax({
url: "<?php echo site_url('main/email_invites'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
return true;
}
});

$.ajax( type: "POST" POST method to php

I'm trying to use the POST method in jQuery to make a data request. So this is the code in the html page:
<form>
Title : <input type="text" size="40" name="title"/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
var title=f.title.value;
$.ajax({
type: "POST",
url: "edit.php",
data: {title:title} ,
success: function(data) {
$('.center').html(data);
}
});
}
</script>
And this is the php code on the server :
<?php
$title = $_POST['title'];
if($title != "")
{
echo $title;
}
?>
The POST request is not made at all and I have no idea why. The files are in the same folder in the wamp www folder so at least the url isn't wrong.
You need to use data: {title: title} to POST it correctly.
In the PHP code you need to echo the value instead of returning it.
Check whether title has any value or not. If not, then retrive the value using Id.
<form>
Title : <input type="text" id="title" size="40" name="title" value = ''/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
var title=jQuery('#title').val();
$.ajax({
type: "POST",
url: "edit.php",
data: {title:title} ,
success: function(data) {
$('.center').html(data);
}
});
}
</script>
Try this code.
In php code, use echo instead of return. Only then, javascript data will have its value.
try this
$(document).on("submit", "#form-data", function(e){
e.preventDefault()
$.ajax({
url: "edit.php",
method: "POST",
data: new FormData(this),
contentType: false,
processData: false,
success: function(data){
$('.center').html(data);
}
})
})
in the form the button needs to be type="submit"
Id advice you to use a bit simplier method -
$.post('edit.php', {title: $('input[name="title"]').val() }, function(resp){
alert(resp);
});
try this one, I just feels its syntax is simplier than the $.ajax's one...
function signIn()
{
var Username = document.getElementById("Username").value;
var Password = document.getElementById("Password").value;
$.ajax({
type: 'POST',
url: "auth_loginCode.jsp",
data: {Username: Username, Password: Password},
success: function (data) {
alert(data.trim());
window.location.reload();
}
});
}
contentType: 'application/x-www-form-urlencoded'

Sending data to php function from ajax

I am using an ajax call which is as follows
var ID=$(this).attr('id');
var input=$("#input_"+ID).val();
var dataString = {id: ID, value: input};
$("#span_"+ID).html(input);
if(input.length>0)
{
$.ajax({
type: "POST",
url: "/apps/worker_app.php",
data: dataString,
cache: false,
success: function(html)
{
$("#span_"+ID).html(span);
}
});
}
How can I get the data in my php function edit_ajax() which is inside worker_app
I use post but the array come to be empty
Thanks
Add dataType: 'json',
var ID=$(this).attr('id');
var input=$("#input_"+ID).val();
var dataString = {id: ID, value: input};
$("#span_"+ID).html(input);
if(input.length>0)
{
$.ajax({
type: "POST",
url: "/apps/worker_app.php",
data: dataString,
dataType: 'json',
cache: false,
success: function(msg)
{
$("#span_"+ID).html(span);
}
});
}
and in worker_app.php get id and value by
$id=$_POST['id'];
$value=$_POST['value'];
edit_ajax($id,$value);
function edit_ajax($id,$value){
$sql="update from ..........";
}
Is this what u want?
Have you tried setting the dataType in your ajax call, like this:
$.ajax({
type:"POST",
url: "/apps/worker_app.php",
data: dataString,
cache: false,
success:function(html) {
},
dataType:"json"
});
Also, you could use a tool like firebug to see so the data is passed correctly in your ajax request.
Here is an example:
file1.php:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javscript"></script>
<script type="text/javascript">
$(document).ready(function() {
// catch submit event for your form
$('#form1').submit(function() {
// serialize data from form
var data = $(this).serialize();
$.ajax({
type="POST",
url:"file2.php",
data:data,
success:function(resp) {
// output response
$('#output').html(resp);
}
});
return false;
});
]);
</script>
</head>
<body>
<div id="output"></div>
<form method="POST" id="form1">
<input type="text" name="name" />
<input type="submit" value="send" />
</form>
</body>
</html>
file2.php:
<?php
if(isset($_POST['name'])) {
header('Content-type: text/html');
echo '<p>Response From Server - Your name is: '.$_POST['name'].'</p>';
}
?>
in /apps/worker_app.php get those posted values
$id=$_POST['id'];
$value=$_POST['value'];
and now use the values with your php function
function edit_ajax($id,$value)
{
echo $id;
echo $value;
}
echo $return=edit_ajax($id,$value);
now you can get the values in the current page as
success: function(html)
{
$("#span_"+ID).html(span);
}
> $.ajax({
> type: "POST",
> url: "/apps/worker_app.php",
> data: dataString,
> cache: false,
> success: function(msg)
> {
> $("#span_"+ID).html(msg);
> }
> });
on success, the argument function is receiving should be same used with
$("#span_"+ID).html(msg);

Modification to add in a DB using PHP and AJAX the ID of the current page

I use a form to post using AJAX to my database the content of the textarea #inpitField. The current url is domain.com/product.php?id=1
Even that I add correctly the data to the DB, I want to post the ID of the page also. I am having some problems though. I have posted the entire code for a better understanding.
Thank you for your help.
In my index.php I have this form.
<form id="tweetForm" action="submit.php" method="post">
<textarea name="inputField" id="inputField"></textarea>
<input class="submitButton inact" name="submit" type="submit" value="update"/>
</form>
I send the content of the textarea #inputField through AJAX. This is the script.js
function tweet()
{
var submitData = $('#tweetForm').serialize();
$('.counter').html('<img src="img/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
$.ajax({
type: "POST",
url: "submit.php",
data: submitData,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('ul.statuses li:first-child').before(msg);
$("ul.statuses:empty").append(msg);
$('#lastTweet').html($('#inputField').val());
$('#inputField').val('');
recount();
}
}
});
}
and my submit.php file is
mysql_query("INSERT INTO offers SET tweet='".$_POST['inputField']."',dt=NOW(),company_id=".$myid.",product_id=".I WANT TO ADD THE PRODUCT ID." ");
function tweet()
{
var urlid = window.location.href.split('?id=', 2)[1];
$('#tweetForm').append('<input type="text" name="id" value="'+urlid+'" />');
var submitData = $('#tweetForm').serialize();
$('.counter').html('<img src="img/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
$.ajax({
type: "POST",
url: "submit.php",
data: submitData,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('ul.statuses li:first-child').before(msg);
$("ul.statuses:empty").append(msg);
$('#lastTweet').html($('#inputField').val());
$('#inputField').val('');
recount();
}
}
});
}
You should be able to append your page id to the data you are posting like this:
submitData = submitData + "&pageid=" + <?php echo $_GET['id'] ?>;
For your code:
function tweet(var pageID)
{
var submitData = $('#tweetForm').serialize();
submitData = submitData + "&pageid=" + pageID;
$('.counter').html('<img src="img/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
$.ajax({
type: "POST",
url: "submit.php",
data: submitData,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('ul.statuses li:first-child').before(msg);
$("ul.statuses:empty").append(msg);
$('#lastTweet').html($('#inputField').val());
$('#inputField').val('');
recount();
}
}
});
}
Called as:
tweet("<?php echo $_GET['id'] ?>")

Categories