-3

I am new to PHP. I have a form like that:

<form action="mailtest.php" method="post" name="mail_form" id="mail_form" >
    ...
 </form>

and mailtest.php has the following code:

<?php
$name=$_POST['form_name'];
if ((!isset($name)) || ($name==''))
{
     echo 'Fill in the name';        
}

else
{
     $name='From '.$_POST['form_name'];
     echo $name;
}
?> 

Now, I want to put inside mailtest.php javascript code which will affect the form in case form_name is null.For example, make a hidden div show up as to indicate the obligation of filling this field or change border color etc. In other words, validation staff I'd have to do with a js file called via onsubmit event. How may I realise this inside my PHP file? Thank you

Unknown developer
  • 4,620
  • 9
  • 34
  • 72
  • Try validation on JS before you submit. If you still get bad data, perform a 302 redirect to original php or something. – Akshaya Shanbhogue Jun 04 '14 at 08:58
  • You might want to check this page http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – naota Jun 04 '14 at 09:01

4 Answers4

0

In short - you can't.

PHP is server side language, JS is client side.

Your JS code must be sent to user before it can be processed.

That's why validation sometimes is divided to server/client parts.

h.s.o.b.s
  • 1,130
  • 6
  • 17
  • My problem is I want to perform server side validation since client side validation is not reliable enough. In which way may I do that and maintain at the same time the figure(messages to the user and some css changes to the form layout) I'd have with a client side validation? – Unknown developer Jun 04 '14 at 09:13
  • Use AJAX calls to validate data on server side and mantain ability to interact with user page. I won't update my answer because it would be too broad, but you can check [this link](http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form) – h.s.o.b.s Jun 04 '14 at 09:18
0

You can make a java script function in source file, not after form submit,

<form action="mailtest.php" method="post" name="mail_form" id="mail_form" onSubmit="return validate()">
    ...
 </form>

You have to give an id to your name text file and then check in java script.

<script type="text/javascript">
function validate()
{
if(document.getElementById("form_name").value == "")
{
alert("Enter name");
return false;
}
}
Pulkit
  • 1
  • My problem is I want to perform server side validation since client side validation is not reliable enough. In which way may I do that and maintain at the same time the figure(messages to the user and some css changes to the form layout) I'd have with a client side validation? – Unknown developer Jun 04 '14 at 09:13
  • you already made the server side verification, you can now redirect page to form page with a get variable and check if get variable is exists then show the error msg. – Pulkit Jun 04 '14 at 09:17
0

PHP is a server side scripting language, it executes the code on server and whatever server side usually works. When you print or echo any statement so it renders on server and you get response in html/other in your browser whatever you print. In your case try to print javascript code like as:

if ((!isset($name)) || ($name==''))
{
  // echo 'Fill in the name';
  echo '<script type="text/javascript">alert("Fill in the name");</script>';        
}

I do not recommend this code just to provide you little help that how it really works. And it's up to you that how you utilize you logic.

kazimt9
  • 553
  • 5
  • 11
  • My problem is I want to perform server side validation since client side validation is not reliable enough. In which way may I do that and maintain at the same time the figure(messages to the user and some css changes to the form layout) I'd have with a client side validation? – Unknown developer Jun 04 '14 at 09:18
0

The easiest way would be to give your object (form_name) an id (lets say form_name), a class that defines the error (say highlight) and make sure you have jquery loaded and then add this:

.highlight {
    border: 1px dotted #cc0000 !important;
}

That should go in your CSS....

<script type="text/javascript">
$(document).ready(function() {
   $('#mail_form').submit((function)() { 
       var field = $('#form_name'); 
       var form_name = $(field).val(); 
       if (form_name == '') { //or whatever else you want to do to validate
           $(field).addClass('highlight'); // would add a class called highlight to the field
           // or toggleClass('hidden') on some field 
           // or any other action you want to check. 
           return false; // stop the form from submitting
       }
   });
});
</script>

... and that would go in your HTML file....

  • This is client side validation. Is it possible to perform server side validation with the graphical effects of the client side? – Unknown developer Jun 04 '14 at 09:22
  • absolutely, but it costs server side cycles and makes the experience worse for the user... you could always do the form as AJAX and post in the background and get the result back from the server that way... personally, I think client side AND server side validation works best, that's how mine is structured. – user3706253 Jun 04 '14 at 09:38