I have the following script, which returns me the following error in my console:
Uncaught SyntaxError: Unexpected token }..
The } in between ** is the one causing the problem according to my console. But that's the bracket which closes the 'success' of the AJAX request.. And also if i remove the statement pointed out with the -> the error seems to disappear. Does someone see what is wrong about this?
Note: I don't have those ** in my code, that's just for pointing out the error.
$(document).ready(function() {
$('#edit_patient_info').click(function () {
//Get the data from all the fields
$.ajax({
url: "patient_info_controller.php",
type: "POST",
data: data,
success: function (msg) {
if (msg==1) {
getPersoonlijkGegevens(user_id);
unLockFirstPage();
alert("Gegevens zijn gewijzigd!");
$("#searchbox").val(voornaam.val());
searchPatient();
-> $('#selectable li:first').addClass('ui-selected');
}
**}**
});
});
});
You had a hidden character after $('#selectable li:first').addClass('ui-selected');
That invalidated your code. Usually, these can be seen when you copy your code to notepad (Or notepad++).
In notepad++, it displayed .addClass('ui-selected');?
Also, you had a extra }.
Try this:
$(document).ready(function() {
$('#edit_patient_info').click(function () {
//Get the data from all the fields
$.ajax({
url: "patient_info_controller.php",
type: "POST",
data: data,
success: function (msg) {
if (msg==1) {
getPersoonlijkGegevens(user_id);
unLockFirstPage();
alert("Gegevens zijn gewijzigd!");
$("#searchbox").val(voornaam.val());
searchPatient();
$('#selectable li:first').addClass('ui-selected');
}
}
});
});
});
From what I can tell it's actually the } two lines down from the one you've marked that's causing the issues; it doesn't match up with any of the opening { characters.
You had an extra }
$(document).ready(function() {
$('#edit_patient_info').click(function() {
//Get the data from all the fields
$.ajax({
url: "patient_info_controller.php",
type: "POST",
data: data,
success: function(msg) {
if (msg == 1) {
getPersoonlijkGegevens(user_id);
unLockFirstPage();
alert("Gegevens zijn gewijzigd!");
$("#searchbox").val(voornaam.val());
}
}
});
});
});
Related
In my dashboard.php I have a Javascript function that is called based on the user clicking a button. When the button is clicked, it calls a JavaScript function called getTeamMembers and values are passed across to it. The values passed across to this function are then sent to a PHP function (which is also located in dashboard.php).
However I am not getting any success and was hoping that someone could guide me on where I am going wrong. I am a noob when it comes to AJAX so I assume I am making a silly mistake.
I know my function is definitely getting the intended variable data passed to it, after doing a quick window.alert(myVar); within the function.
This is what I have so far:
function getTeamMembers(teamID,lecturer_id) {
var functionName = 'loadTeamMembersChart';
jQuery.ajax({
type: "POST",
url: 'dashboard.php',
dataType: 'json',
data: { functionName: 'loadTeamMembersChart', teamID: teamID, lecturer_id: lecturer_id },
success: function(){
alert("OK");
},
fail: function(error) {
console.log(error);
},
always: function(response) {
console.log(response);
}
}
);
}
Before calling the desired php function, I collect the sent varaibles just before my php Dashboard class starts at the top of the file. I plan to pass the variables across once I can be sure that they are actually there.
However, when I click the button, nothing can be echo'd from the sent data.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if (!empty($_POST["teamID"]) && !empty($_POST["lecturer_id"]))
{
$teamID = $_POST['teamID'];
$lecturer_id = $_POST['lecturer_id'];
echo $teamID;
echo " is your teamID";
}
else
{
echo "no teamID supplied";
}
}
you else statement in you success function , and that's not how you set fail callback, try the following and tell us what do you see in the console.
function getTeamMembers(teamID,lecturer_id) {
jQuery.ajax({
type: "POST",
url: 'dashboard.php',
dataType: 'json',
data: {functionname: 'loadTeamMembersChart', arguments: [teamID, lecturer_id]},
success: function(data) {
console.log(data);
},
fail: function(error) {
console.log(error);
},
always: function(response) {
console.log(response);
}
});
}
Seems like your function is not returning a success message when getting into the following statement.
if ($result) {
"Awesome, it worked"
}
Please try to add a return before the string.
if ($result) {
return "Awesome, it worked";
}
It can be other factors, but the information you provided is not enough to make any further analysis.
I am using the following code to get data from an input field and send it to PHP by POST but its not working
<script type="text/javascript">
$(document).ready(function () {
$("#id_1").change(function () {
var rat1 = $(this).val();
$.ajax({
url: "upload.php",
type: "post",
data: rat1,
success: function (response) {
// you will get response from your php page (what you echo or print)
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});
</script>
this is the input form
<input type="number" name="your_awesome_parameter" id="id_1" class="rating" data-clearable="remove"
data-icon-lib="fa" data-active-icon="fa-heart" data-inactive-icon="fa-heart-o"
data-clearable-icon="fa-trash-o"/>
You need to provide a name for the parameter. It should be:
data: { param_name: rat1 }
Then in upload.php you access it with $_POST['param_name']
Just in case, did you imported Jquery into your project?
I tested your code and I with the minor change that Barmar specified and it is working for me.
Try to use this code in your php file and see if you get any response in the developer tools console.
$data = $_POST["param_name"];
echo json_encode([$data]);
Try in this way men
function realizaProceso(valorCaja1, valorCaja2){
var parametros = {
"valorCaja1" : valorCaja1,
"valorCaja2" : valorCaja2
};
$.ajax({
data: parametros,
url: 'ejemplo_ajax_proceso.php',
type: 'post',
beforeSend: function () {
$("#resultado").html("Procesando, espere por favor...");
},
success: function (response) {
$("#resultado").html(response);
}
});
}
change on input type number is not working in older versions of browsers, I think not sure. But try this below solution as you are using input type number.
$("#id_1").on("mouseup keyup",function () {
//your logic here
});
and passing data as already mentioned by others:
data: { param_name: rat1 }
I need to add autocomplete to an input textbox. The data needs to be fetched from SharePoint using AJAX / REST.
This what I've done so far:
JS
var myData = [];
var requestHeaders = {
"accept": "application/json;odata=verbose"
}
$.ajax({
url: "https://my-URL/sites/RMA-GFPLC/_api/web/lists/GetByTitle('AD_DB')/items? $select=Title,Regional_x0020_Office,Commodity,Commodity_x0020_Year,StateLookUp/Title&$expand=StateLookUp",
type: 'GET',
dataType: 'json',
async: false,
headers: requestHeaders,
success: function (data) {
$.each(data.d.results, function (i, result) {
myData.push(result.Title);
});
myDataSource(myData);
},
error: function ajaxError(response) {
alert(response.status + ' ' + response.statusText);
}
});
function myDataSource(myData){
$('#myAutoCompleteSearch').autocomplete({
source: myData,
minLength: 3
});
}
So far my code is not working, and I'm getting "Uncaught TypeError: Cannot read property 'label' of null " error in my console. I;m wonder what am I doing wrong here? Thanks!
This error occurs when a source for Autocomplete function contains an element(s) with a null value.
Solution
Add the condition for checking if value is not null:
$.each(data.d.results, function (i, result) {
if(result.Title) {
myData.push(result.Title);
}
});
Jast insert your code in
$(document).ready(function () {
//your code here
}
I have the following json response:
{"data":[{"series":{"id":"15404","series_code":"TOS","publisher_id":"280","series_short_name":"Tales
of
Suspense","start_year":"1959","end_year":"1968","published":"1959-1968","type_id":"1","no_issues":"99","published_gcd":"January
1959-March 1968","series_long_name":"Tales of Suspense (Marvel
1959-1968)","volume":"1","first_issue":"1","last_issue":"99","comment":"","created_date":"2013-03-24
01:00:00","user_id":"1","last_updated":"2013-03-24
01:00:00","note":"","is_active":"1","wiki_stem":null}}],"error":"boo"}
Here is the ajax call:
$.ajax({
type: 'get',
url: '/series/lookup?year='+json.IssueYear+'&title='+json.Title,
beforeSend: function(xhr) {xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
},
success: function(response) {
if (response.error) {
alert(response.error);
console.log(response.error);
}
else {
//console.log(response.content);
alert(response.data[0].series.id);
}
},
error: function(e) {
alert("An error occurred: " + e.responseText.message);
console.log(e);
}
});
I would have thought that this would return the id but it says data is undefined?
alert(response.data[0].series.id)
I must be missing something, any help appreciated.
Need a little bit more information. How are you getting the response back to your code? if this was JQuery, I know that this would work:
$.post("/path/to/script.php", {
var1: 'test',
var2: 'test2'
}, function(response) {
alert(response.data[0].series.id); // This will work here
}, 'json');
That said, this little snippet shows what would work. Without know what your javascript code is, it makes it very near impossible to determine if what you are doing is right or wrong.
Can any body help me out.
I have this code
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function GOTO() {
var datastring = "id=2";
$.ajax({
type: "POST",
url: "/Home/Index",
dataType: "json",
data: datastring,
success: function (json) { Complete(json.result); },
error: function (request, status, error) {
//alert("an error occurred: " + error);
alert("Error saving data. Please contact support.");
}
});
}
function Complete(result) {
if (result == "success") {
alert("Success");
}
else {
alert("Failed");
}
}
</script>
<input type="button" value="submit" onclick="JavaScript:GOTO()" />
</asp:Content>
and My Controller Code is this
[HttpPost]
public System.Web.Mvc.JsonResult Index(string datastring)
{
return Json(new { foo = "bar", baz = "Blech" });
}
But it never hits my controller at all, is that Something I am doing wrong in my View?
thanks
Try passing the data like this:
$.ajax({
type: "POST",
url: "/Home/Index",
dataType: "json",
data: { datastring: 'foo bar' },
success: function (json) { Complete(json.result); },
error: function (request, status, error) {
alert("Error saving data. Please contact support.");
}
});
function Complete(result) {
if (result == "success") {
altert("Success");
// ^ beware!
You have a syntax error in your code.
Moreover, you don't need to specify the JavaScript: protocol in the onclick attribute, you're merely defining a label at that place. Even better, don't assign event listeners in HTML attributes at all, but use unobtrusive JavaScript, including a fallback for the rare case when JavaScript is unavailable.
Update: if you get the "$.ajax is undefined" error, you probably didn't include jQuery in your page. Add
<script type="text/javascript" src="path/to/jquery-X.X.X.js"></script>
To your page above the script element that uses $.ajax (or any other jQuery function).
it could be down to what data you are passing in. Your method expects a parameter called datastring but you are passing in 'id=2'.