1

I am trying to set a value of a productID field by searching the product name. The search returns the correct results and displays them but when clicking the value is being set as if it is a number.

For exmaple this is what is returned when searching...

pID: "<a class='result' onclick='setID(0001-001-004)' href='#'>Product Code=0001-001-004, White Folding Chair</a>"

When the below result is clicked, the value of the input is set as -4. If the number returned was 0001-002-005 it gets set to -5.

The code for the setID function is...

function setID(pID) {
    var prodCode=toString(pID);     
    $('input#productCode').val(pID);
    var clear="";
    $('div#suggestions').html(clear);
    $('div#suggestions').hide(); 

}  

Does anyone have any idea how I can do this. I can not return the id between double or single quotations becasue this is the php that returns the result. as you will see the use of double and single quotes leaves me no way to do it in this way...

$array[] = array (
        "pID" =>"<a class='result' onclick='setID(".$row['ITEMNO'].")' href='#'>Product Code=". $row['ITEMNO'].", " .$row['ITEMDESC']."</a>"
    );

3 Answers 3

2

You need to add single quotes around the concatenation, like this

$array[] = array (
        "pID" =>"<a class='result' onclick='setID(\'".$row['ITEMNO']."\')' href='#'>Product Code=". $row['ITEMNO'].", " .$row['ITEMDESC']."</a>"
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, you're update is still wrong : "SyntaxError: illegal character" on click.
1

You're missing double quotes around your value in setID() :

"pID" =>"<a class='result' onclick='setID(\"".$row['ITEMNO']."\")' href='#'>Product Code=". $row['ITEMNO'].", " .$row['ITEMDESC']."</a>"

Because, values are interpreted as numbers : 0001-002-005 = 1-2-5 = -6
instead of "0001-002-005" which is a string.

Or using single quotes (to use double quotes in HTML) :

"pID" => '<a class="result" onclick="setID(\''.$row['ITEMNO'].'\')" href="#">Product Code='. $row['ITEMNO'].', ' .$row['ITEMDESC'].'</a>'

5 Comments

Got it in one! Thank you so much! I knew i had to wrap it quotes but didn't know how as there was already a mix of double and single quotations.
@AKAust I've updated the answer to show you the same string using single quotes.
That's why it may be better to use data-* attributes and define the event listener in the JavaScript and not the HTML.
Agree with @Dormilich. Also consider using json_encode() instead of manually printing quotes, then you won't need to worry about it being a number or a string (or any other type) and it will be safer against XSS. And don't forget to escape HTML (e.g. htmlspecialchars())
Mind that json_encode() only works in HTML attributes when single quotes are used (and for HTML double quotes are more common).
1

when clicking the value is being set as if it is a number

Because in JS you have set it as a number, but you need to set it as string:

<a class='result' onclick='setID("0001-001-004")'>Product Code=0001-001-004, White Folding Chair</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.