Hi all, I have this ajax script that accepts only one value at a time. I want to send more than one value to mysql for processing... but am not so good with javascript, pls point me in the right way....thanks

// auto select vehicle # and fleet #
        <script>
                function showFleet_Loc(str)
                {
                if (str=="")
                  {
                  document.getElementById("fleet_loc_Hint").innerHTML="";
                  return;
                  } 
                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 (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                    document.getElementById("fleet_loc_Hint").innerHTML=xmlhttp.responseText;
                    }
                  }
                xmlhttp.open("GET","fleet_loc_select.php?q="+str,true);
                xmlhttp.send();
                }
             </script>

Here is fleet_loc_select.php

                $q=$_GET["q"];

                $cd ="UPDATE vehicles SET image_file='$newimage' WHERE svreg='$q'"; 
                mysql_query($cd);                   

            ?>

Dani AI

Generated

Short practical summary that builds on , and : sending multiple values is straightforward — either as multiple query parameters or (preferably for updates) in the request body — but updates should not be done with a GET because GET is meant to be safe/read-only. Use a POST (or PUT) and send a small payload (JSON or form data) from the client; handle it server-side with a prepared statement rather than the old mysql_* calls. (httpwg.org)

A minimal modern client pattern (uses Fetch; FormData is an alternative when files are involved):

// build a payload with as many fields as needed
const payload = { svreg: sv, fleetId: fleetId, image_file: fileName };

fetch('fleet_loc_select.php', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(payload)
})
  .then(res => res.json())
  .then(resp => { /* check resp.success */ })
  .catch(err => console.error(err));

Fetch is the recommended modern replacement for XMLHttpRequest; FormData can be used when uploading files. (developer.mozilla.org)

Server-side: read the JSON or form fields and use PDO with a prepared statement to avoid injection and to be compatible with current PHP versions (ext/mysql is removed). Example pattern:

$data = json_decode(file_get_contents('php://input'), true);
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('UPDATE vehicles SET image_file = :img, fleet = :fleet WHERE svreg = :svreg');
$stmt->execute([':img'=>$data['image_file'], ':fleet'=>$data['fleetId'], ':svreg'=>$data['svreg']]);
echo json_encode(['success' => true]);

The old mysql_* functions are deprecated/removed; prefer PDO (or mysqli) and always use parameterized queries. Prepared statements are the recommended protection against SQL injection. (php.net)

Quick troubleshooting reminders: inspect the Network panel to confirm payload and headers, log/return a JSON error message when something fails, set PDO to throw exceptions during development, and validate/sanitize every input on the server. This approach follows ’s PDO suggestion and gives a cleaner, safer alternative to the original single-parameter GET used by .

Recommended Answers

All 5 Replies

Do you want to use jquery?
Do you like to provide functionality for users with javascript disabled?
Can you use pdo?

These are the question you should answer before going forward

And to help you with your question you can send as many inputs form a form via jquery ajax to the db and retrieve them to see how visit this link form the documentaion
http://api.jquery.com/jQuery.ajax/
Hope this helps

There are several options...if you are going to use plain JavaScript/AJAX, you can send multiple values using the query string approach if you are sending via GET, or you use POST as well. both have the ability to send mutliple values. If you use POST, you have to adjust your PHP code to retrieve POST parameters.

GET

xmlhttp.open("GET","fleet_loc_select.php?q1="+str1+"q2="+str2,true);
xmlhttp.send();

POST

xmlhttp.open("POST","fllet_loc_select.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q1="+str1+"&q2="+str2);

As Dani said, you could try using jQuery also, it has all of these functionalities enable right out of the box.

thanks for your help, its just that am very comfortable with this script. it works fine with no problem, i will take your advice, thax...

thaxs Szabi Zsoldos

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.