I'm submitting a form with jQuery .. and in order to manage the post() i have to escape() some text area..
my jQuery script:
var dataString = 'title=' + escape(title) + '&auth=' + auth + '&mainImage=' + mainImage + '&art_text=' + escape(art_text) + '&language=' + language + '&tags=' + tags + '&Status=' + Status + '&postDate=' + datepicker;
$.post("./Scripts/adminDoAddPost.php", dataString, function (data) {
//do something
});
now when i get the data .. i reaches my php script as
while it should be displayed as من نحن
is there a way to decode the whole string?
You can use urldecode and html_entity_decode as
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($_GET['whatever']));
$result = html_entity_decode($str,null,'UTF-8');
Related
I'm pretty new to web programming so, making a logic for a web-page, I made a mistake and now I'm in trouble.
I named some divs like "Dv_1.2.1.3" (without knowing the problems linked to using the dot) and I have issues trying to clone (via jquery called by button) some of these.
The button id contains the id of the div I want to clone, so, my logic is:
1) extract the id of the div;
2) get the div and clone it (giving a new id).
I'm stuck with getting the div because of the dots in the id.
The below code is what I've done so far:
$('.CloneDiv').click(function () {
var SplittedId = (this.id).split('_');
if (SplittedId[0]=='Clone'){
alert('SplittedId 1 =' + SplittedId[1]);
//Modify id to use it to find the div to clone
var UsableId = SplittedId[1].replace(/\./g, '\\\\.');
alert('UsableId =' + UsableId);
//Count existing elements
var ClonedNum = $('#' + 'Dv_' + UsableId + '_').length;
ClonedNum++;
var OrigElem = $('#' + 'Dv_' + UsableId).length;
alert('OrigElem =' + OrigElem); //THIS IS 0
//NO ELEMENTS FOUND BUT THE ELEMENT EXISTS
//Clone the element and give new id
var ClonedElem = $('#' + 'Dv_' + UsableId).clone().attr('id', function( i, val ) {
return val + '_' + ClonedNum;
});
ClonedElem.find("input").val("");
if (ClonedNum > 1){
ClonedNum--;
var AnteId = '#' + 'Dv_' + UsableId + '_' + ClonedNum;
alert(AnteId);
$(AnteId).after(ClonedElem);
}else{
var AnteId = '#' + 'Dv_' + UsableId;
alert('AnteId = ' + AnteId);
$(AnteId).after(ClonedElem);
};
}else if(SplittedId[0]=='Del'){
alert(SplittedId[0]);
alert('Del');
}else{
//error
};
});
Might these help: developer.mozilla.org/en-US/docs/Web/API/CSS/escape , Polyfill: github.com/mathiasbynens/CSS.escape/blob/master/css.escape.js
I am building a facility on a website (using Symfony2) that allows the user to create reports and display them on a screen using AJAX. The reports are effectively SQL statements that are created on the fly and are then ran on the Database.
The problem I have is that I can't fathom a way to display these results to the screen without first knowing what fields are used in the report. The queries could contain just 2 fields from a table, or 15 fields, and I'd like the code to be robust enough to handle this.
So far, this is the code I'm using:
$.ajax({
type: 'POST',
url: urlLink,
success: function (data) {
var Type = (data.recordType);
var Results = (data.results);
var Name = (data.name);
var Description = (data.description);
var Titles = (data.titles);
$('#reportName').text(Name);
$('#reportDescription').text(Description);
$('#listTable > tbody:last').empty();
$('#listTable > thead:last').empty();
$('#listTable > thead:last').append('<tr>'+Titles+'</tr>');
$.each(Results, function(i, item) {
$('#listTable > tbody:last').append('<tr><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td></tr>');
});
}
});
The variable Titles comes from the query, as when the user is adding fields to the database these are then added to a string which I then explode using PHP in the controller.
Inside the foreach, every column comes back with [object Object]. When I remove the [i] from the code and replace it with .column-name it will then work. But this is what I'm trying to avoid. I'd like to have something similar to what I do with the Table Titles.
Maybe try this, and show output of console.log(data);
console.log(data);
var data = $.parseJSON(data);
$.each(data, function() {
$.each(this, function(k, v) {
/// do stuff
});
});
I am making a simple jQuery mobile app, where one of the pages is about a restaurant.
On that page, I want to have a button that, when clicked, saves the information of the restaurant to a database.
I already have a php code that can save data to my database, and i have this javascript code that works together with it.
Though this is used for saving data from a google maps infowindow when closed. I just need a on click button.
var url = "phpsqlinfo_addrow.php?name=" + name + "&address=" + address + "&description=" + description + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
downloadUrl(url, function (data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
document.getElementById("message").innerHTML = "Location added.";
}
}
What is the easiest approach to this?
Thanks.
EDIT:
Is it possible to do like this, if i create a button with id #btn_save :
$(document).ready(function() {
$(document).on("click","#btn_save",function() {
"phpsqlinfo_addrow.php?name=" + john + "&address=" + lolstreet 12 +
"&description=" + this is restaurant john + "&lat=" + xx.xx() +
"&lng=" + yy.yy();
}); });
Sorry, i know i should be learning the basics first.. :/
If I understood correctly, you need to add button onClick event handler? If so, here you go (using jquery 1.9+):
$(document).ready(function() {
//onClick event handler for button with id "btn_save"
$(document).on("click","#btn_save",function() {
//Your code here
});
});
I am trying to pass the time zone parameter in a Ajax request
Like
alert(meeting_time);
alert(meeting_timezone);
$.ajax({
type: "GET",
url: "http://webfaction/UI/user/joinmeeting.php",
data: "user_names="+ user_names + "&image=" +file+"&duration="+ duration +"&user_name="+ user_name + "&meeting_id=" + meeting_id + "&moderator_password=" + moderator_password +"&attendee_password=" + attendee_password +
"&meeting_time=" + meeting_time + "&meeting_timezone=" + meeting_timezone + "&meeting_sms_no=" + meeting_sms_no + "&meeting_logout_url=" + meeting_logout_url +"&meeting_maxp=" + meeting_maxp +"&meeting_name=" + meeting_name ,
success: function(json){
alert(json);
$('#show_ajax').hide();
if(!json.error) location.reload(true);
},
Now my problem is before ajax request
alert(meeting_timezone); is printing `GMT +8.00`
but on the joinmeeting.php page
$meeting_timezone = $_GET['meeting_timezone'];
print $meeting_timezone;
the above print statement is printing the value GMT 8.00
(+ sign is not printing here)
Please tell me how to fix this problem ?
That's a known situation while using POST and GET methods, your + signs will be converted to space. The right way to pass data through POST or GET is using encodeURIComponent in JavaScript.
For example in your case, do this before sending:
meeting_timezone = encodeURIComponent(meeting_timezone);
It will help if you encode your URL in JavaScript by
encodeURIComponent(uri)
Like this:
data: encodeURIComponent(("user_names="+ user_names + "&image=" +file+"&duration="+ duration +"&user_name="+ user_name + "&meeting_id=" + meeting_id + "&moderator_password=" + moderator_password +"&attendee_password=" + attendee_password +
"&meeting_time=" + meeting_time + "&meeting_timezone=" + meeting_timezone + "&meeting_sms_no=" + meeting_sms_no + "&meeting_logout_url=" + meeting_logout_url +"&meeting_maxp=" + meeting_maxp +"&meeting_name=" + meeting_name )
Also take a look at this question:
How to encode a URL in Javascript?
might be useful
Instead of specifying the data: parameter as a string, use an object:
data: { user_names: user_names,
image: file,
...
meeting_timezone: meeting_timezone,
...
}
jQuery will then take care of converting it to a query string properly.
I have been working on a website that uses a combination of PHP, jQuery, MySQL and XHTML in order to register students for a piano recital. This has no official purpose other than a learning exercise for me in getting all of these to work together. However, I have had a lot of problems getting the PHP to talk with the database and I'm not sure what my problem is. But before that can be tackled there is a really annoying issue that I've run across. For some reason my jQuery is not building a complete post URL for the PHP.
I am using jQuery version: 1.4.2 from Google. The query string is being built by using:
var ajaxOpts = {
type: "post",
url: "../php/addRecital.php",
data: "&performance=" + $("#performanceType :selected").text() +
"&groupName=" + $("#groupName").val() +
"&student1fName=" + $("#firstName").val() +
"&student1lname=" + $("#lastName").val() +
"&student1id=" + $("#studentID").val() +
"&student2fname=" + $("#Second_Student_firstName").val() +
"&student2lname=" + $("#Second_Student_lastName").val() +
"&student2id=" + $("#Second_Student_studentID").val() +
"&skillSelect=" + $("#skillSelect :selected").text() +
"&instrument1=" + $("#instument1 :selected").text() +
"&otherInstrument1=" + $("#otherInstrument1").val() +
"&instrument2=" + $("#Instument2 :selected").text() +
"&otherInstrument2=" + $("#otherInstrument2").val() +
"&location=" + $("#locationSelect :selected").text() +
"&roomNumber=" + $("#roomNumber").val() +
"&time=" + $("#timeSlotSelect :selected").text()
,
success: function(data) { ...
There is more than the above function, but I didn't think that it would pertain to here. I then call the code using:
$.ajax(ajaxOpts);
However, instead of creating the entire query string I get:
http://sterrcs123.mezoka.com/schoolProject/assign/assign13.html?groupName=&firstName=Samuel&lastName=Terrazas&studentID=23-343-3434&Second_Student_firstName=&Second_Student_lastName=&Second_Student_studentID=&otherInstrument=&Second_Student_Instrument=&roomNumber=2
Which as you can tell is missing a number of keys and their values. I would appreciate any help I can get because this is really driving me insane. Thanks.
It appears that your form is simply submitting itself without using your AJAX operation. Did you attach to the form's submit event and THEN run your ajax call? You will also want to return false from the submit event handler to prevent the default behavior you are seeing above.
Example:
$('#formid').submit(function(){
//your ajax code here.
return false;
});
If you're talking to an ASP.Net web service then you would need data to be a JSON string of your arguments. Otherwise, you can pass your data in by using an object literal (truncated for brevity):
var ajaxOpts = {
type: "post",
url: "../php/addRecital.php",
data: {
performance: $("#performanceType :selected").text(),
groupName: $("#groupName").val(),
student1fName: $("#firstName").val(),
student1lname: $("#lastName").val()
},
success: function(data)
}
As per the missing values, make sure you're capturing the button click / form submit.
$('form').submit(function (e) {
$.ajax(ajaxOpts);
return false;
});