Hello there, anyone can help me with my problem?

my problem is, i want to display a "please complete all the fields" when the user inputs incomplete data in the textboxes and i want to display it in the same form.

im new in php.

These are my codes

<?php
		
		$posapp = $_POST['txtapplied']; 
		$fname = $_POST['txtfname']; 
		$mname = $_POST['txtmname']; 
		$lname = $_POST['txtlname']; 
		$month=$_POST['txtmonth'];
		$day=$_POST['txtday'];
		$year=$_POST['txtyear'];
		$age=$_POST['txtage'];
		$eadd=$_POST['txtemail'];
		$homenumber=$_POST['txthnum'];
		$cpnumber=$_POST['txtcpnumber'];
		$address=$_POST['txtaddress'];
		$religion=$_POST['txtreligion'];
		$birthplace=$_POST['txtbplace'];
		$height=$_POST['txtheight'];
		$weight=$_POST['txtweight'];
		$civilstatus=$_POST['txtcstatus'];
		$college=$_POST['txtcollege'];
		$course=$_POST['txtcourse'];
		$yeargrad=$_POST['txtyeargrad'];
		
		$db=mysql_connect('localhost','root','') or die ('Cannot connect to MYSQL!');
		@mysql_select_db('dbriomra') or die('Cannot connect to database');

		
		$query="INSERT INTO onlineapplication (positionapplied, firstname, middlename, lastname, month, day, year, age, emailaddress, homenumber, cellphone, address, religion, birthplace, height, weight, civilstatus, college, course, yeargrad) VALUES ('$posapp', '$fname', '$mname', '$lname', '$month', '$day', '$year', '$age', '$eadd', '$homenumber', '$cpnumber', '$address', '$religion', '$birthplace', '$height', '$weight', '$civilstatus', '$college', '$course', '$yeargrad')";

       
  //$result = mysql_query($query); 
  
  if(mysql_query($query))
        {
         header('Location: oapp_finished.php');
        }
        else
        {
         echo "<br> Problem Loading";
        }
     
       mysql_close($db);
?>

Dani AI

Generated

OP wanted the form to show "Please complete all required fields" inside the same page. Combine a lightweight client-side check for immediate feedback with a strict server-side check for safety, and migrate away from legacy mysql_* usage to parameterized queries (PDO or MySQLi) β€” the old ext/mysql was deprecated and removed from modern PHP versions. (php.net)

A simple, non-intrusive client-side pattern uses HTML5 required plus the Constraint Validation API to surface a single message in the form and still let the browser highlight the missing inputs:

<form id="appForm" novalidate>
  <div id="formError" style="color:red"></div>
  <input name="firstname" required>
  <input name="email" type="email" required>
  <button type="submit">Send</button>
</form>

<script>
document.getElementById('appForm').addEventListener('submit', function(e){
  if (!this.checkValidity()) {
    e.preventDefault();
    document.getElementById('formError').textContent = 'Please complete all required fields.';
    this.reportValidity(); // highlights individual controls
  }
});
</script>

The Constraint Validation API handles the browser-side checks and custom messages. Use it for UX, not as the only defense. (developer.mozilla.org)

Server-side must re-check everything and insert safely. Example pattern (trim/validate inputs, use filter_var for email, escape HTML when re-populating the form, and use PDO prepared statements to write to the database):

// validate inputs
$first = trim($_POST['firstname'] ?? '');
$email = trim($_POST['email'] ?? '');
$errors = [];
if ($first === '') $errors['firstname'] = 'Required';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors['email'] = 'Invalid email';

// on success, use PDO prepared statement
$pdo = new PDO('mysql:host=127.0.0.1;dbname=app;charset=utf8mb4','user','pass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->prepare('INSERT INTO applicants (firstname,email) VALUES (?, ?)');
$stmt->execute([$first, $email]);

Use filter_var for validation and htmlspecialchars when echoing old values back into inputs. Prefer parameterized queries over manual escaping to prevent SQL injection. Also follow least-privilege DB accounts and set PDO to throw exceptions for easier debugging. (php.net)

Notes tied to earlier replies: was right to steer toward client-side JS for UX; 's security prompt is important β€” escaping alone is fragile, so validate and use parameterized queries. Adapt field names and error placement to the form layout so messages appear next to the offending controls when re-rendering the form.

Recommended Answers

All 4 Replies

Post your validation code...

Post your validation code...

Karthnik_pranas , i want to display a "Please complete all the required fields". but i don't know where i need to put it. i'm new in php, can you help me?

And make sure you secure those $_POST[] :S

for instance.. $posapp = mysql_real_escape_string($_POST);
I could easily mess up your database with a simple injection. Dont ever trust the classic "they dont know how this code look" thought.

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.