Cannot send POST request with jQuery and AJAX - javascript

I am trying to send POST data to my php file notify.php. For that I am using:
$.post("notify.php", { phone : phone_number });
phone_number is the input name in HTML. But the POST isn't getting sent. I used Firebug firefox plugin to check AJAX error and got:
TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement.
...i+"["+k+"]",n)}):f(i,o)}function f(i,o){o=c.isFunction(o)?o():o;e[e.length]=enco...
What is wrong?

You need to set the variable before using it, else it will give error in console and send an undefined value to the AJAX server.
var phone_number = $('#phone_number').val();
This assumes the input is defined like:
<input type="text" id="phone_number">

Try setting the variable like this first: phone_number = $('input[name=phone_number]').val()

Related

How to read data when sent by DELETE method?

I am firt time trying DELETE method with fetch in javascript. I just want to read the sent data from my fetch in php. I am doing this:
... in js
fetch(this.url+"?deleteID="+ID,{method:'DELETE'})
... in php
if(($_SERVER['REQUEST_METHOD'] == 'DELETE')){
$id= $_GET["deleteID"];
}
but it is failing and I do not know why. The definition of $_GET is "An associative array of variables passed to the current script via the URL parameters" and I am passing deleteID as a param or not ? (When the method is GET, this works)
Thanks for explanation.
Im sorry, this works... I was using wrong URL

How do we get a div attribute from a jquery.post response?

I have set up two documents on my server, "index.php" and "proc.php". In the "index.php", I wrote some JavaScript code that will send a post request to "proc.php". The proc.php sends a successful response, in the form of a div:
<div id="serverResponseDiv" data-class="new-class">
The request was successful, and this is the response.
</div>
When I attempt to get/console.log the "data-class" attribute from the server response, I get an error "undefined".
// index.php script
...
var pst=$.post(site,obj, function(){
//alert('good');
} );
pst.done(function(data) {
$("#indexDiv").empty().html(data);
var nClass=$("#serverResponseDiv").attr('data-class');
console.log(nClass); // for debugging purposes.
$("#indexDiv").removeClass("old-class").addClass(nClass);
});
...
How do I get the attribute value from a server response div? I have done multiple google searches, but cant find the answer to my question. Please help.
You should not use the "data" functionality as a attribute.
Try another ways like class or custom attributes.
I you can not do it, try this:
var nClass = $('#serverResponseDiv[data-class="newClass"]');

How to Pass Variables from an External php script to Google App script

I'm really new to Google Appscript, so please forgive me if this question sounds stupid.
I am trying to past variables from a PHP script into a Google App Script to utilise Gmail to send emails. I'm not quite sure if I am going about it the right way, but I'm trying to pass the variables to the appscript via a get request in the URL and access them that way.
//get the variables from the url and send email
function doGet(e){
var first_name = e.parameter.first_name;
var last_name = e.parameter.last_name;
var email = e.parameter.email;
var phone = e.parameter.phone;
GmailApp.sendEmail(first_name,last_name + email, phone);
}
doGet();
I tested it's ability to get the variables from the url via a get request by deploying it as a web app and passing some test variables in the URL:
https://script.google.com/macros/s/AKfycbzk14ZKDdsofFqx0vU2_kFIXLTduAMvy_G_9MyuS_d046MZIGQb/exec?first_name=testname&last_name=testname&phone=testname&email=testname
However, I get the following error on the page:
TypeError: Cannot read property "parameter" from undefined. (line 24, file "Code", project "email 6")
Could you please tell me what I am doing wrong here or is there an even better way to solve this sort of problem.
Thanks!
Remove the call to doGet() that you've placed after the function definition.
doGet() will be called automatically when the HTTP request is received, you don't need to call it explicitly.
When the HTTP request is received, any code not within a function is executed before doGet() is triggered. This means your current code is running doGet() with no parameters, thus hitting the "e" is undefined error, before the actual HTTP request is ever passed to doGet.
Otherwise the code in your function looks correct.

Python Error When Accessing JSON Data From AngularJS POST

I am trying to send data to a python script via POST but unable to read it. The UI is in AngularJS. I have seen similar questions but they are either not using AngularJS or not using POST.
The Angular code:
$scope.send = function(){
$data = {
"tableData":$scope.personsSelected
}
$postData = JSON.stringify($data);
Services.post('testbed.cgi', $postData);
}
Python Code:
formData = cgi.FieldStorage()
print json.dumps(formData)
Error Message:
TypeError: FieldStorage(None, None, []) is not JSON serializable
I have tried setting the content type to application/x-www-form-urlencoded but to no avail. What am I missing here?
The issue is with the data you are trying to send as JSON. Whats the data you are trying to send? Is it a Python object? This may help you. Are those plain strings? Dictionary? Do they have invalid characters? You can use JSON Lint to check validity.

Unable to get the variable in sqlite query Phonegap

Please have a look. I am getting an error of "Uncaught type error: can not read the property 'title'".
here is how code looks like :
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO post_details(post_title, post_date,post_comment,post_content,post_categories,post_image) VALUES ("'+data.posts[i].title+'","'+data.posts[i].date+'","'+data.posts[i].comment_count+'","'+data.posts[i].content+'","'+data.posts[i].categories+'","'+data.posts[i].thumbnail_images.full.url+'")',[], nullHandler,errorHandler);
});
This "data" is a variable holding JSON data which i am calling through AJAX. Which is returning properly if am normally alert it.
Thank you!

Categories