$ClassName=mysql_real_escape_string($_POST["ClassName"], $con);
$query="SELECT ClassID FROM Class WHERE Name='".$ClassName."' ";
$q=mysql_query($query,$con); its not working..any help...please............!!!!!!!!!
$ClassName=mysql_real_escape_string($_POST["ClassName"], $con);
$query="SELECT ClassID FROM Class WHERE Name='".$ClassName."' ";
$q=mysql_query($query,$con); its not working..any help...please............!!!!!!!!!
A few focused checks will save time. The symptom you describe can come from three places: the POST value never reaches the PHP script (AJAX encoding/client-side), the database call never actually runs or returns rows, or the code tests the result incorrectly. was right to ask for the actual error output, correctly pointed to client-side encoding issues, and warned against copy/paste—use those clues to debug step by step.
Quick reproducible checks (server-side)
Confirm the PHP script actually receives the form field. Temporarily return the raw POST so you see what the server got:
header('Content-Type: text/plain');
var_export($_POST);
exit; Confirm a working DB connection before escaping or querying. If you see the expected POST but no rows, the problem is with the SQL or data mismatch (whitespace, case, charset).
Avoid fragile escaping and use prepared statements (example with PDO)
$db = new PDO('mysql:host=HOST;dbname=DB;charset=utf8mb4','USER','PASS',[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $db->prepare('SELECT ClassID FROM `Class` WHERE `Name` = :name');
$stmt->execute([':name' => trim($_POST['ClassName'] ?? '')]);
$result = $stmt->fetch(PDO::FETCH_ASSOC); Simple AJAX POST example using modern Fetch (client-side)
const fd = new FormData();
fd.append('ClassName', document.querySelector('#className').value);
fetch('handler.php', { method: 'POST', body: fd })
.then(r => r.text())
.then(console.log); Extra tips: trim the input before querying, check table/column names for case-sensitivity on Linux, confirm connection charset (use utf8mb4), and make sure you check the query result before fetching rows. The old mysql_* extension is deprecated—migrate to PDO/mysqli for prepared statements and better error handling (mysql_real_escape_string docs, PDO prepared statements, Using Fetch API).
Jump to Post— pritaeas 2,276$q = mysql_query($query, $con) or die(mysql_error());Be more specific.
Jump to Post— pritaeas 2,276Post the error you get here. "Not working" is too vague.
Jump to Post— Sorcher 6You wont learn much by receiving full scripted codes.. Copy&Paste is not good practice.
Start here;
http://www.w3schools.com/php/php_ajax_database.asp
$q = mysql_query($query, $con) or die(mysql_error()); Be more specific.
i have used thz too
$q = mysql_query($query, $con) or die(mysql_error()); its still not working still ((
Post the error you get here. "Not working" is too vague.
when i used this it works
$query="SELECT * FROM Class WHERE Name='".$ClassName."' ";
$row=mysql_fetch_row($q); can u tel me a good tutorial of implementing ajax with php?
Try w3schools.
Every time you use JavaScript to pass information to a PHP page you need to make sure to encode the strings. And also to add slashes before sending to Database.
Normally the problem relies in JavaScript handling string information. If the string includes and &(ampersand) will break the string into two different variables. The same if the string has quotes or single quotes.
If you're not receiving and error from the mysql_error() then the problem is in the JavaScript not the PHP.
i want the code of passing form elements with post in php with ajax..
i am trying a number of codes,but all in vain:(
can anyone send me please, full code
You wont learn much by receiving full scripted codes.. Copy&Paste is not good practice.
Start here;
http://www.w3schools.com/php/php_ajax_database.asp
I agree with Sorcher. If you have enough bandwith there is "e-teacher" on youtube
The best tutorials in my opinion can be found here;
and also if you are pretty new with PHP - MYSQL you should look up Phpacademy on youtube.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.