1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Calling url, I only get "" as value of parameter passed to PHP by JavaScript

Discussion in 'PHP' started by JoshuaEir, Jan 15, 2021.

  1. #1
    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
     
    Solved! View solution.
    Last edited: Jan 15, 2021
    JoshuaEir, Jan 15, 2021 IP
  2. #2
    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?
     

    Attached Files:

    JEET, Jan 15, 2021 IP
  3. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #3

    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.
     
    Last edited: Jan 15, 2021
    JoshuaEir, Jan 15, 2021 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    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...
     
    JEET, Jan 16, 2021 IP
  5. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #5
    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.
     
    JoshuaEir, Jan 17, 2021 IP
    JEET likes this.