PDA

View Full Version : Javascript / PHP. Result only displays until it reaches a space??


tux
01-25-2010, 07:48 PM
I've been experimenting with Spry after watching Davidj's vids.

All has been well but I have come across a problem.

I am calling a PHP script using a Javascript (see below). The script calls a query from my database and display it in a input field. The problem is the result should be this...

9"x6" Print, 12"x10" Mount

... but only displays this....

9"x6"

I thought it may be the quotes used as inch symbols foxing the script but removing them made no difference.

I have tried different outputs and the display from the query always stops when it reaches the first gap or space. eg. Stops at the gap between 9"x6"and Print.

My scripts are below, any idea why this is anyone!

Regards, Paul

PHP

<?php

require_once("Connection/PTPconnection.php"); // database connection

$productID = $_GET['productID'];

//build query
$query = sprintf("SELECT * FROM products WHERE productID = '$productID'");
$result = @mysql_query($query);
$product = @mysql_fetch_array($result);

//Build Result String
$editProduct = "<div class=\"form_line\"><label for=\"edit_product\">Edit Product</label>";
$editProduct .= "<input name=\"edit_product\" type=\"text\" id=\"edit_product\" value=".$product['productName']." onkeyup=\"updateCounter(this, 40, 'title', 'countArea1');\" onpaste=\"updateCounter(this, 40, 'title', 'countArea1');\"/>";
$editProduct .= "<div class=\"small_text\"><b id=\"countArea1\">40</b> Characters Left.</div>";
$editProduct .= "</div>";
$editProduct .= "<div class=\"form_line\"><label for=\"product_price\">Edit Price</label>";
$editProduct .= "<input name=\"product_price\" type=\"text\" id=\"product_price\" value=".$product['productPrice']." />";
$editProduct .= "<div class=\"small_text\">This format: 0.00 Pound sign not required.</div>";
$editProduct .= "</div>";
$editProduct .= "<div class=\"form_button\">";
$editProduct .= "<input type=\"image\" name=\"edit\" value=\"edit\" src=\"Images/Buttons/button_edit.gif\" alt=\"Edit Product\" title=\"Edit Product\" />";
$editProduct .= "</div>";
$editProduct .= "<input type=\"hidden\" name=\"productID\" id=\"productID\" value=".$productID." />";
$editProduct .= "<input type=\"hidden\" name=\"submitted\" id=\"submitted\" value=\"1\" />";

echo $editProduct;

?>

Javascript

// function to edit product

function chooseProductFunction(){

var ajaxRequest; // magic variable

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}

// Receive Data Function
ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('edit_product');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}

}

var productID = document.getElementById('productID').value;

var queryString = "?productID=" + productID;
ajaxRequest.open("GET", "edit_product_process2.php" + queryString, true);
ajaxRequest.send(null);
}

tux
01-26-2010, 05:46 PM
Maybe this will help you guys shed some light on this for me!

If I replace the value in this.....

$editProduct .= "<input name=\"edit_product\" type=\"text\" id=\"edit_product\" value=".$product['productName']." onkeyup=\"updateCounter(this, 40, 'title', 'countArea1');\" onpaste=\"updateCounter(this, 40, 'title', 'countArea1');\"/>";


with just some text....

$editProduct .= "<input name=\"edit_product\" type=\"text\" id=\"edit_product\" value=\"some text\" onkeyup=\"updateCounter(this, 40, 'title', 'countArea1');\" onpaste=\"updateCounter(this, 40, 'title', 'countArea1');\"/>";

it displays okay, therefore I take it the problem is with concatenating this.....

$product['productName']

into the string. But why??

Cheers all.

davidj
01-28-2010, 10:29 AM
you didn't escape the value quotes

value=".$product['productName']."

tux
01-28-2010, 06:53 PM
Hi Dj,

I did try escaping them but the script threw its toys out of the pram.

I did it like that as it was like that in your tut....

$dropdown .= "<option value=".$rowModel['car_model_id'].">".$rowModel['car_model']."</option>";

Do you think I should try breaking the line of code down into smaller bits.

It works without them but only shows the DB result until the first gap/space in the text.

Another minor thing was I tried to display a pound sign () but it displayed a small square!! I tried using '&pound' but it still show the small square. Why would that be?

$editProduct .= "<div class=\"small_text\">This format: 0.00 Pound sign () is not required.</div>";

davidj
01-28-2010, 07:51 PM
you're missing something...

value=".$product['productName']."should be

value=\"".$product['productName']."\"

tux
01-28-2010, 08:35 PM
Cheers for that David, I'll give it a try.

What do you think about the sign not displaying??

davidj
01-28-2010, 08:57 PM
try using the encoded value of

&pound;

tux
01-28-2010, 09:09 PM
I tried '&pound' and '£' but have just realised that I missed the semi colon (;) at the end.

I'll give it another go later but would the semi colon throw the script. Would it need escaping?

Cheers mate.

davidj
01-29-2010, 03:49 PM
give it a try