Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

how can change Ajax call from GET to POST method?

Dear Experts,

I use PHP 8, 

How can I use below ajax call with post method?

this is GET method, it works.

but I need to do it with POST method.

Do you recommend new or secure version of it?


thank you


function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
        document.getElementById("seh").innerHTML = str;
        
    }
}

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will also have to change the server code to use the $_POST object
Avatar of BR

ASKER

Dear Michel Plungjan,
that is a great answer.

One more help please,
in PHP file, how can I get below output in Json format?
thank you som much

while ($cikti = $sorgu->fetch(PDO::FETCH_ASSOC)) {
            

            echo "<option value=". $cikti['ilcesi'] . ">" . mb_strtoupper($cikti['ilcesi']) . "</option>" ;
            
        }
        echo "</select><br><select name=bayitipi required class='form-control text-uppercase mr-0' style='max-width:350px;'>
        <option value=2>TÜM BAYİLER</option>
        <option value=1>smth1</option>
        <option value=2>smth2</option>
        <option value=5>ŞOK Market</option>
        </select><br><button type=submit class='btn btn btn-warning transition-3d-hover font-weight-normal ml-0 mb-3'>show</button>";
        
    }

Open in new window

thank you

You can convert the data to JSON using a PHP function named json_encode() (https://www.php.net/manual/en/function.json-encode.php)

$sql_query = mysql_query("SELECT ilcesi, col2, col3 FROM ilcesi_table");
$results = array();
while($row = mysql_fetch_array($sql_query))
{
   $results[] = array(
      'ilcesi' => $row['ilcesi'],
      'extra1' => $row['col2'],
      'extra2' => $row['col3']
   );
}
$json = json_encode($results);

Open in new window

That will return your result from the database as a JSON object.


Please note that I just commented on the server part, the answer was given by Mlanda T
Avatar of BR

ASKER

Dear Mlanda T,
thank you for your great answer,
Thank you Michel reminding me that first answer was given by Mlanda T,
I am working on the json part, I will let you know..
thank you