javascript post

javascript   (jquery edit in place)
get
values

some values have
&
character

which is a param separator

so how to use
&
LVL 1
rgb192Asked:
Who is Participating?
 
hieloCommented:
Make sure you encode the data before you send/post it back to the server.  For example, let's say that you have two input fields:

Company: AT&T
Category: Phone

in your editor, if you now want to send it to the server via ajax you can do:
<script>
var data="";
data+=company=encodeURIComponent(document.getElementById('company').value);
data+="&"+category=encodeURIComponent(document.getElementById('category').value);

//here you should see the data with the ampersand in "AT&T" encoded
alert( data );

//at this point you can do your ajax call and send what you have in data
...
</script>

Open in new window

0
 
Julian HansenCommented:
?
0
 
Chris StanyonCommented:
I'm with Julian ?

In your header you mention POST, in your post you mention GET. Can you explain a little more about what you want to achieve.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Sar1973Commented:
In Javascript you can find the "&" character in several case (for example && is the AND operator in logical tests), while in HTML it is often used to refer to a single character by its code (es. &#38 is the & itself).
See also http://www.w3schools.com/tags/ref_ascii.asp, http://www.w3schools.com/js/js_operators.asp,
http://msdn.microsoft.com/en-us/library/yek4tbz0(v=vs.94)
If you want to use it as a single character inside a string, you could otherwise than using its code putting it inside single quotes ('&').
0
 
aplusexpertCommented:
Please refer this link.
http://stackoverflow.com/questions/5020345/how-to-escape-ampersand-character-in-form-input-using-jquery

or you can use following code for getting value using Jquery
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("input:text").val());
  });
});
</script>
</head>
<body>
Firstname: <input type="text" name="fname" value="Peter" /><br />
Lastname: <input type="text" name="lname" value="Griffin" /><br /><br />
<button>Return the value of the first text field</button>
</body>
</html>

Open in new window

0
 
Julian HansenCommented:
I think the author is asking how to deal with an & in data that is then ajax'd back.

There should be no issue with this - but as the question was phrased so badly it is difficult to tell what is actually required - hence my initial response in kind.
Consider the following code
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
	$('#go').click(function() {
		$.post('t106.php', {term: $('#select').val()}, function(data) {
			$('#result').html(data);
		});
	});
});
</script>
<style type="text/css">
</style>
</head>
<body>
Input here <input type="text" id="select" />
<input type="button" id="go" />
<div id="result"></div>
</body>
</html>

Open in new window

Calling this script
<pre>
<?php
print_r($_POST);
?>
</pre>

Open in new window

With this input
This is & &amp; test

Open in new window

The result is
<pre>
Array
(
    [term] => This is & &amp; test
)
</pre>

Open in new window

Not sure if that answers the question
0
 
Julian HansenCommented:
@hielo - if you send data back like so

$.post('theurl.php',{key: value, ...} ...)

JQuery will encode the values you for you.

If you go the string concatenation route (which you shouldn't) you will need to manually encode.
0
 
hieloCommented:
@julianH:
Yes, I'm aware of jQuery's capabilities, but thanks for that anyway - the intention is appreciated.  

From his post it sounds like he is already concatenating, but doesn't know how to deal with the "&".  I was just giving a non-jquery example so emphasize the general solution to the problem (and demystify his problem), which like you well know requires one "... to manually encode".

Regards,
Hielo
0
 
Julian HansenCommented:
@hielo - apologies - did not mean to insinuate you did not know the above (should have left the @hielo off) was directing at the author. A case of starting a response in one direction, changing my mind and not going back to amend the start bit.
0
 
hieloCommented:
apology not necessary.  Like I said "the intention is appreciated." - honestly.
0
 
rgb192Author Commented:
thanks

I think hielo is on the right track

I apologize and did not ask the question properly

I have another question with source code

maybe you can apply your answers to this question

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27854680.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.