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

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);
}
});
}

Related

Use AJAX Return value to PHP function

My ajax successfully return the value but my problem is I can't use the value to my function. How do I convert this div to a value?
<?php
echo $ajax_user_id = '<div id="result"></div>'; //value can display here
getName($ajax_user_id); //value wont work here
?>
<form method="post" id="registerSubmit">
<input type="text" name="user_id" id="user_id">
<button id="submit" type="submit">Submit</button>
</form>
<script>
$(document).ready(function(){
$("#registerSubmit").submit(function( e ) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "post",
data: $(this).serialize(),
dataType: "text",
success: function(Result) {
$('#result').text(Result)
}
})
});
});
</script>
You can't use it that way.because jQuery is a scripting language which runs in browser when DOM is fully loaded and elements are available to make selectors.While php is server side language which runs on server way before page load.anyway to fulfill your need you can try something like this.
<form method="post" id="registerSubmit">
<input type="text" name="user_id" id="user_id">
<button id="submit" type="submit">Submit</button>
</form>
<script>
$(document).ready(function(){
$("#registerSubmit").submit(function( e ) {
e.preventDefault();
$.ajax({
url: "test.php",
method: "post",
data: $(this).serialize(),
dataType: "text",
success: function(Result) {
$('#result').text(Result);
getNameAjax(Result);
}
})
});
});
function getNameAjax(val)
{
$.ajax({
type: "POST",
url: GetNameAjax.php,
data:{'user_id':val},
success: function(res){
$('#name').html(res);
}
});
}
</script>
GetNameAjax.php file you can write like this
$ajax_user_id=$_POST['user_id'];
function getName($ajax_user_id)
{
return "name on the basis of id";
}

How to display the AJAX response in input field and pass that value to PHP?

I am getting Id value from my AJAX and I am also able to display it. I set the Id value input text in the AJAX and displaying it on HTML like this <input value="4864" id="compare_id" type="hidden"> Now I have to pass that value to PHP to get the result from database.
Is there any idea how to do it?
AJAX
$(document).ready(function() {
arr = [];
$("[type=checkbox]").click(function() {
$.ajax({
type: "POST",
url: "includes/compare_process.php", //
data: 'users=' + arr,
dataType: 'json',
success: function(msg) {
$("#pics_name,#pics_Id").empty();
$.each(msg, function() {
$("#pics_Id").append("<input type=hidden value=" + this.Id + " id=compare_id name=compare_id>");
//alert(msg.id);
});
},
error: function() {
alert("failure");
}
});
});
});
//tried AJAX
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: "GET",
url: 'includes/compare_process.php?key=compare_details',
data: $('#search-form').serialize(),
success: function(response) {
//$('#response').html(response);
alert(response);
}
} );
});
});
PHP
<div class="bg-popup" style="display: none;" id="open_compare_popup">
//popup code here
<?php
$val2=htmlentities($_GET['compare_id']);
echo $val2;
$sql = "SELECT * FROM `request` WHERE Id IN($val2)";
?>
//end popup code here
This output I am getting from AJAX
<form action="#" method="get" id="search-form">
<span id="pics_Id">
<input value="4869" id="compare_id" name="compare_id" type="hidden">//output
<input value="4884" id="compare_id" name="compare_id" type="hidden">//output
<input value="5010" id="compare_id" name="compare_id" type="hidden">//output
</span>
<button class="action action--button action--compare" type="button" id="search-button" name="search-button"><span class="action__text">Compare</span></button>
</form>
I have to pass the input value to PHP after clicking on a button.

pass value from view to controller in codeigniter using onclick

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

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'

Categories