Store ajax data to php - php

I have a function.I want Data passing through ajax is store in php variable.I tried below code but not work please some one help me.
function moreinfo(prodid,catid,price,type,catname) {
url2="<?php echo $this- >getUrl('compatibility/compatiblelist/moredetails'); ?>";
$j.ajax({
url:url2,
type: 'POST',
data: {"prodid": prodid},
success: function(response) {alert(console.log(response));}
});
<?php
$ms = $_POST["prodid"];
echo $ms;
?>
}

<?php ?> tags only work when file name must be .php.
may you are using that process in .js file kindly change the ext

consider separating the file, .js and .php then you can include .js file

function moreinfo(prodid,catid,price,type,catname) {
url2="something.php";
$.ajax({
url:url2,
async:false,
type:'POST',
data: {prodid: prodid},
dataType:'html',
success: function(response) {alert(console.log(response));}
});
}
In your something.php
<?php
$ms = $_POST["prodid"];
echo $ms;
?>

Related

jQuery & PHP to delete a file

Got this following code:
HTML:
Delete
JS:
$('.deleteFile').click(function(){
if (confirm("Delete file ?")){
$imagefile = $(this).attr('id');
$.ajax({
type: 'POST',
data: {
action: 'deleteFile',
imagefile: $imagefile,
},
url: 'assets/php/deleteFile.php',
success: function(msg) {
alert($imagefile);
}
})
}
})
PHP:
if($_POST["action"]=="deleteFile") {
$imagefile = $_POST['imagefile'];
unlink("files/1/".$imagefile);
}
I do not why it does not work. My file is still here...
Could you please help me?
Thanks.
HTML:
Delete
PHP
unlink($SERVER['DOCUMENT_ROOT']."/files/1/".$imagefile);
You need to open the php declaration before writing a php variable:
<?php echo $entry ?>
Your html code must look like:
Delete
You need to save the file as php.
I think you need to echo $entry variable here
Delete

Need help in passing value form JS to PHP

I have some problem with passing value from javascript file to php file.
I make some calculations in JS, and after that i passing it to php.
This is a code of JS:
var price=100;// for example
$.ajax({
type: "POST",
url: "SecureRide.php",
data: { calculatedPrice: price }
});
And this is a code of PHP:
<?php
session_name("ridePrice");
session_start();
$_SESSION["calculatedPrice"]=$_POST["calculatedPrice"];
echo $_SESSION["calculatedPrice"];
?>
So what i do is a simple Ajax function that passes the value to php, very simple, but doesn't work!
When I do echo function there's nothing there.
Maybe there are another way to solve it?
Thank you very much!
Note : if you are put your ajax code in function and than call function from the "$(document).ready(function(){" then your code run perfectly
I write code in your .js file :
$(document).ready(function(){
functionName(price);
});
function functionName(price){
$.ajax({
url: 'SecureRide.php',
data:{calculatedPrice: price},
type: 'POST',
success:function(data)
{
alert(data);
},
error:function(data)
{
alert("mistake in your code");
}
})
}
And Code For PHP file for get .JS File Data
if(isset($_POST['calculatedPrice']) && !empty($_POST['calculatedPrice']))
{
$calculatedPrice1=$_POST['calculatedPrice'];
}

CodeIgniter, Ajax call does not get into Controller

After looking at a solution on ci-ajax-csrf-problem I added the following line into the script and it works fine.
var post_data = {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}
inserted into
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
data: post_data,
Thank you for the help everyone :)
I am new to Ajax/Jquery and was following a guide on Ajax for CodeIgniter from jorge torres to implement a simple ajax call on my website and ran into problems.
I created a Controller Ajax and this is the code snippet.
class Ajax extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
$output_string = 'This is a test';
echo json_encode($output_string);
}
public function test2(){
$this->load->view('test.php');
}
}
And this is the view for that controller, its identical to the one from the tutorial except I added loaded the url helper $this->load->helper('url'); on the first line
Here is the snippet for the script code.
The #getdata is a button type and #result_table is a div
$('#getdata').click(function(){
$.ajax({
url: '<?php echo base_url().'ajax/test';?>',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
I can successfully access localhost.com/codeigniter/ajax/test2 but when I clicked the button, nothing happen.
I tried looking at the page source info and the url is correct
$.ajax({
url: 'http://localhost/codeigniter/ajax/test',
type:'POST'
....
Accessing localhost/codeigniter/ajax/test directly is also possible and it display the output message.
I am using CodeIgniter 2.1.3 and my localhost is running on XAMPP 1.7.3
Thank you in advance :)
After looking at a solution on ci-ajax-csrf-problem I added the following line into the script and it works fine.
var post_data = {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}
inserted into
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
data: post_data,
Thank you for the help everyone :)
Did your .click() event handler works as expected?
$('#getdata').click(function(){
alert('clicked');
........
if no, try to wrapped the jquery code in $(document).ready(function() { ... }); as #Sudhir suggested:
$(document).ready( function() {
$('#getdata').click(function(){
alert('clicked');
........
});
if yes, in case you don't know it yet, there's a tool called firebug to check your AJAX request.
I thinks there's no problem with your AJAX url.
So far, this is my answer. Hope this helps too.
Do you have output compression enabled(gzip) in your config.php? If you compress your output it will fail when you use echo and return the 500 server errors.
I think there is an issue with your ajax call . Should be like this I think :
$(document).ready (function () {
$('#getdata').click(function(){
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
});
If the firebug console says that The action you require is not allowed, then it may be a CSRF token issue, turn it off in the codeigniter config.
Add this in config file.
$config['csrf_protection'] = FALSE;
Hope this helps :)
Have you tried
$(document).ready (function () {
$('#getdata').click(function(){
$.ajax({
url: '/ajax/test',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
});
as apposed to <?php base_url; ?>
Have you entered your site url into the CI config?

jQuery ajax returning nothing when posting multiple data

hello I would like to post multiple data to a php file with jQuery ajax but when I execute the function it's retuning nothing and the php also doesn't get the data
my function looks like this:
function sendForm(){
jQuery.ajax({
url: <?php echo $path; //this the url where the php file is ?>,
type: 'POST',
data: {
addressOne: jQuery('#Form #table .key :input').serialize(),
addressTwo: jQuery('#Form #table_2 .key :input').serialize(),
additionalData: jQuery('#Form #More :input').serialize(),
preloaded: <?php echo serialize($array); ?>,
action: 'sendIt'
},
async: false,
cache: false,
success: function(data){
alert(data); //or console.log(data);
}
});
}
and in the php I do something like this:
<?php
function handleData() {
parse_str($_POST['addressOne'], $arrayOne);
parse_str($_POST['addressTwo'], $arrayTwo);
parse_str($_POST['additionalData'], $arrayThree);
$preloaded = unserialise($_POST['preloaded']);
//than do some stuf here for example print_r all...
}
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'sendIt' : handleData();
break;
//etc...
}
}
?>
I don't know what I'm doing wrong? Is there a better way to post multiple data?
If I use only one data like I'm serialising the whole form and do not post the serialized php array than it works fine, but I would like to use this four separate data.
You mistyped your ajax query url with ulr
Consider using a plugin for your browser like Web Developer, I'm pretty sure it would have picked up the error and you wouldn't have needed to ask here.
Edit: if you are still having troubles, validate the data you are going to send with some alert, validate that your php script does what you want it to do by navigating to it manually from the browser, since you provide a success callback, any reason you're not async, etc... validate everything
You got a typo in the ajax function,
url: <?php echo $path; ?>, //needs to be url, not ulr
You also don't need to serialize the values from the text fields. Just do this,
jQuery.ajax({
url: <?php echo $path; //this the url where the php file is ?>,
type: 'POST',
data: {
addressOne: $('#Form #table .key :input').val(),
addressTwo: $('#Form #table_2 .key :input').val(),
additionalData: $('#Form #More :input').val(),
preloaded: <?php echo serialize($array); ?>,
action: 'sendIt'
},
async: false,
cache: false,
success: function(data){
alert(data); //or console.log(data);
}
});

How do I add a php variable to this jquery url setting?

I'm trying to make the URL in the following example contain a php variable like this:
http://api.crunchbase.com/v/1/company/$company.js
This is my jquery code:
$.ajax({
url: "http://api.crunchbase.com/v/1/company/airbnb.js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}
What do I need to do to achieve this?
Thanks!
You'll need to output the variable as JavaScript from PHP at some point in the script. This can be done quite easily using json_encode:
var company = <?php echo json_encode($company) ?>;
Note that this will have to be placed in a file that is actually executed by PHP, rather than an inert .js file. One way to achieve this is to put it in an inline <script> in the HTML in your PHP template; e.g.
<script type="text/javascript">
var company = <?php echo json_encode($company) ?>;
</script>
Try this:
<script type="text/javascript">
var company = <?php echo json_encode($company); ?>;
alert(company);
</script>
You're setting the company variable with the output of $company, you can then use this variable however you please in JavaScript.
$.ajax({
url: "http://api.crunchbase.com/v/1/company/" + company + ".js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}
Can you try this, assuming you have the company name in the php $company variable:
$.ajax({
url: "http://api.crunchbase.com/v/1/company/<?php echo $company ?>.js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
}

Categories