Well, of course: you've jammed everything together into a string, which means you have to escape the parameters. + and & are both special in HTTP URL syntax. The appropriate escape function is encodeURIComponent(), but easier would just to be to avoid passing things around as strings in the first place:
Code:
function editField(id, field) {
var defaultVal = document.getElementById(field).innerHTML,
inputVal = prompt("Please enter a new value",
defaultVal);
if (inputVal && inputVal != defaultVal) // Check if empty, cancelled or value not changed
new Ajax.Request("editjob.php", {
onComplete: refreshField,
method: 'post',
parameters: {
id: id,
val: inputVal,
field: field
}
});
}
It will presumably work after that, but it can still be improved in several ways. The first is the use of innerHTML. innerHTML is non-standard and should be avoided where possible. In this case, it looks like you're actually dealing with a form element. If you are, you can use .value instead:
Code:
function editField(id, field) {
var f = document.getElementById(field),
inputVal = prompt("Please enter a new value", f.value);
if (inputVal && inputVal != f.defaultValue) // Check if empty, cancelled or value not changed
new Ajax.Request("editjob.php", {
onComplete: refreshField,
method: 'post',
parameters: {
id: id,
val: inputVal,
field: field
}
});
}
The second is that big clumsy switch statement. Switch statements are rarely useful in Javascript: usually we can use arrays or objects instead:
Code:
var f = document.getElementById(["customername",
"customeraddress",
"customerphone",
"customerfinish",
"customeremail"][req.responseText.charAt(0) - 1]);
(f.firstChild || f.appendChild(document.createTextNode("")))
.nodeValue = req.responseText.substring(1);
Note that again I've removed the innerHTML usage there; if you really did need to send a big chunk of HTML over the network, you should rethink your design.
If you're just starting out with Javascript, I really don't recommend Prototype.js to get you started. It is exemplary, but only, I fear, as a warning of how not to code.
Additionally, we don't really use this style very much in Javascript:
Code:
controlKeyword(foo)
{
bar;
}
This is becauseis a block, and it is perfectly valid standing on its own. The only way we can visually distinguish that style from a function call followed by a block is by checking for a semicolon at the end of the control statement, and since semicolons are optional in Javascript, even that isn't reliable. Rather, we both separate the keyword from the following brackets with a space and put the opening brace on the same line as the control flow statement:
Code:
controlKeyword (foo) {
bar;
}
We also indent the contents of the block, so we know at which level the contents belong (I noticed you didn't do that with your switch statement). Of course, if you have only one statement affected by the control flow statement, you needn't bother with the braces at all: you can simply indent it properly and make it much clearer to what each statement belongs (since it always belongs to the control flow statement immediately above it).
Fear not: there is no AJAX in the code you provided above (you're using a simple text format, not XML). 
My recommendation to use PDO on the PHP side of things stands.
Bookmarks