Using Ajax to Post PHP variables to another file - php

I want to post the PHP variables $uid and $submissionid to the file fblike.php. Is the Ajax below formatted correctly to do this?
<?php
ob_start();
session_start();
$uid = $_SESSION['loginid'];
$submissionid = mysql_real_escape_string($_GET['submissionid']);
$_SESSION['submissionid'] = $submissionid;
?>
<head>
<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
$("a.connect_widget_like_button").live(function(){
$.ajax({
type: "POST",
data: "action=vote_up&uid="+$(this).attr("uid")"&submissionid="+$(this).attr("submissionid"),
url: "fblike.php",
});
});
});
</script>
</head>

You dont really want to use expando attributes if you dont have to, especially since thise are links... i would jsut do:
Like
then you can do a simple:
$("a.connect_widget_like_button").live('click', function(e){
e.preventDefault();
$.post($(this).attr('href'));
});
Now on the php side you need to be aware of where the values will be. If you pass the values as i have done in my example they will be in $_GET (even if its a POST request). If you pass them like you did in your original post then they will be in $_POST.

You need to send the data as an array/object. Something like this should do the trick.
$(function(){
$("a.connect_widget_like_button").live(function(){
$.ajax({
type: "POST",
data: {
action: "vote_up",
uid: $(this).attr('uid'),
submissionid: $(this).attr('submissionid')
},
url: "fblike.php"
});
});
});

Related

Send data of a form using serialize function

i want to send all the input fields of the form to process/do_submitattendance.php,
where i can use the to store in the database.
However i am having trouble doing this.
My jQuery code is-
<script type="text/javascript">
$("#submitattendance").submit(function(){
var_form_data=$(this).serialize();
$.ajax({
type: "POST",
url: "process/do_submitattendance.php",
data: var_form_data,
success: function(msg){
alert("data saved" + msg);
});
});
</script>
submitattendance is the ID of the form element.
I'm guessing the form is submitting, and you'll have to prevent the default submit action:
<script type="text/javascript">
$(function() {
$("#submitattendance").on('submit', function(e){
e.preventDefault();
$.ajax({
type: "POST",
url : "process/do_submitattendance.php",
data: $(this).serialize()
}).done(function(msg) {
alert("data saved" + msg);
});
});
});
</script>
var var_form_data=$(this).serialize();
is all i can find, or there must be an error in your code somewhere else. you can look in your chrome console to see if there is an error (f12). also add return false; to stop the form submitting itself.
Why don't you try to pass values through session?
By using session you can pass the values from one page to anyother pages you want.
the typical code looks like this:
Mainpage.php
<?php
session_start(); // You should start session
$_SESSION['UserName']="YourTextfield";
?>
SecondPage.php
<?php
session_start();
$var = $_SESSION['UserName'];
?>
After you saved the data...then you need reset the session
$_SESSION['UserName']="";
That's what I usually use. and I hope it will help you...

My ajax/jquery script to send $_GET to php isn't working? (Wordpress)

This is the code that I'm using to send a variable (via GET) to another php file:
(basically, I click on a button, and then js gets the id and sends the id via ajax to the php file.
$(document).ready(function() {
$(".doClick").click(function() {
var category=$(this).attr('id');
$.ajax({
url:'aFile.php',
type:'GET',
data: $category,
success: function(data){
alert("It worked?"); // this is the response
}
});
alert($(this).attr("id"));
});
});
This is the code in my aFile.php:
The php file gets the info via $_GET[] and then assigns it to a variable and uses that variable in a function call.
<head>
<script type="text/javascript">
$(document).ready(function() {
function JS() {
//code
});
</script>
</head>
<body onload="JS()">
<?php
$category = $_GET['category'];
if (function_exists('inventory_insert')) {
echo inventory_insert('{category_name = '.$category.'}');
} else echo('warning');
?>
It's supposed to give me a response back on my main page, but nothing seems to be happening. I don't even get the alert I posted after the ajax script.
your variable is category but you're sending data: $category
You must send key/value pair to server
to receive $_GET['category'] your data sent in ajax needs to be either:
data: 'category='+category
Or
data: {category: category}
You have assigned id into category in jquery. so correct in data params.
data: {category : category},
Send it in this way to server or php file.
$(document).ready(function() {
$(".doClick").click(function() {
var category=$(this).attr('id');
$.ajax({
url:'aFile.php',
type:'GET',
data: {category : category},
success: function(data){
alert("It worked?"); // this is the response
}
});
alert($(this).attr("id"));
});
});

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

jquery.ajax post get issue

I am new to studying jquery.ajax. Through some tutorials, I tried some code by myself, but I met some trouble. So I ask for help.
I tried to do this: open a.php, send html data from div#send to b.php, then return the data and show in div#result.
a.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function () {
var params = "value=" + $('#send').text();
$.ajax({
url:'b.php',
type:'post',
dataType:'html',
data:params,
success:data
});
function data (html) {
var str=html.send;
alert(html);
$("#result").html(str);
}
});
</script>
<body>
<div id="result"></div>// I need return 'this is an apple.'
<div id="send">apple</div>
b.php
<?php
echo 'This is an '.$_REQUEST['value'].'.';
?>
You are not sending any valid parameters with the ajax request. If you want the textual contents of #send you need to use:
$('#send').text();
Although that only gives you a string, so it would have to be:
var params = "value=" + $('#send').text();
Apart from that, $_REQUEST is an array, so you have to use something like $_REQUEST['value']
A third point is that your success function is too complicated and wrong (html.send does not exist), it could just be:
success: function(msg){
$("#result").html(msg);
}
To return datas from b.php, you have to do an echo

Executing a php file onClick

I currently have a php file executing:
test
I would rather not have to load a new page and do it onClick.
Does anyone know a simple way to do this?
I added a more complete example, including the suggested ajax. I still am having trouble getting it it to work though.
<script type="text/javascript">
$('li').click(function() {
$.ajax({ type: "GET", url: "test.php", data: { foo: 'boo' }, success: function(data){
// use this if you want to process the returned data
alert('complete, returned:' + data);
}});
});
</script>
</head>
<body>
<div id="header">
<h1>Title</h1>
</div>
<ul>
<li>Test</li>
</ul>
</body>
</html>
you could use jquery and do something like this:
$.ajax({ url: "test.php", data: { foo: 'boo' }, success: function(data){
// use this if you want to process the returned data
// alert('complete, returned:' + data);
}});
for more information, take a look at the jquery-documentation
Yes, as your tags say. You will need to use AJAX.
For example:
$.ajax({
type: "GET",
url: "test.php",
data: "foo=boo"
});
Then you just have to stick that within a click function.
$('a').click(function() {
// The previous function here...
});
EDIT: Changed the method to GET.
In your firl say e.g try.html write this code
<meta http-equiv="refresh" content="2;url=test.php?foo=boo">
it will redirect u to test.php?foo=boo in 2 seconds
OR Other way ===========================================
create one php file
<?php
header('Location: test.php?foo=boo');
exit;
?>
Hope this help

Categories