Using PHP, I can't get JavaScript to call the URL with string parameters correctly. I have tried passing the parameter from PHP and also getting the value using an input id. I have worked very hard at it. $btitleID holds a value correctly. PHP: $string1 .= " <div class=\"container\"> <div class=\"row\" > <div class=\"col\"> <h4><center><p id = \"\">Image</p></center></h4> <center><p id = \"\"> $image </p></center> </div> <div class=\"col\"> <h4><center><p id =\"\" >Title</p></center></h4> <center> <p > <input id = \"$btitleID\" value = \"$title1\" type=\"text\" name=\"title\" placeholder=\"\"></p></center> </div> Code (markup): <center><button id = \"\" onclick = \"displayAddProductChanges($productID, '{$btitleID}', '{$bdescID}', '{$bcostID}','{$bquantityID}','{$bkey1ID}' , '{$bkey2ID}' , '{$bkey3ID}','{$gKeyword1}','{$gKeyword2}','{$gKeyword3}', '{$image}', '{$title1}', '{$description}', $cost , $quantity, $keywordID, $category )\">Submit</button></center> PHP: JavaScript: function displayAddProductChanges(productID, btitleID, bdescID, bcostID,bquantityID,bkey1ID , bkey2ID , bkey3ID, gKeyword1, gKeyword2, gKeyword3 , image, title1, description, cost , quantity, keywordID, category) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { title1 = document.getElementById (btitleID).value; if (this.readyState == 4 && this.status == 200) { var jsonData = JSON.parse(this.responseText); var answerHtml = jsonData.htmlstuff; } }; var url = "displayAddProductChanges.php?title1=" ; url = url + title1; xmlhttp.open("GET", url , true); xmlhttp.send(); } Code (JavaScript): Thank you, Josh
This is working good for me. Do you have something in $title1 or not? I am attaching a cut down form of your code. Save it as php file and run in your localhost. On clicking the submit button, it will alert the value of input box. This is same code as yours, I just removed the ajax call code, cause I was just testing parameter passing part... What is the error you are getting?
this is displayAddProductChanges.php : <?php $title1 = $_GET['title1']; PHP: if $title1 is set than the alert contains the correct altered text. However, the $_GET code just displays the original value that the $title was set as. For example, if I inserted "a different text" and the original text was "this is first text" the alert box would display "a different text" and the $_Get would still be "this is first text". As a side note, the javascript function would only work if the parameter names were set the same as the php (without the $). Perhaps this is not right and will shed some light on the subject.
Your problem is not clear, but I think ajax call is caching result which is why you are seeing same result again and again. Do this in your JS code where you are declaring "url": var url = "displayAddProductChanges.php?title1=" ; url = url + title1; url = url + "&r="+ Math.random(); //continue rest of code...
Well, JEET, that didn't work. However, I did retype some code and compare and got the program to work. It is disturbing, though, not knowing why exactly it wasn't working. I tell myself, what's important is that it works and I'll analyze later.