Hello friends, I have a html form saved as join.html as below

<form id="form1" name="form1" method="post" action="check.php">
  Name:
  <label>
  <input name="name" type="text" id="name" />
  </label>
  <p>Email: 
    <label>
    <input name="email" type="text" id="email" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
</p>
</form>

and a php file saved as ckeck.php as below;

<?php
if(!empty($_POST[Submit]))
{
$name=mysql_real_escape_string($_POST[name]);
$email=mysql_real_escape_string($_POST[name]);
if($name=="")
echo "Enter your Name.<br>";
elseif ($email=="")
echo "Enter your Email.<br>";
else
echo "Thank you.";
}
?>

If any of the field is empty, I will be directed to check.php to see the error. I will want the error message to be echoed at the top of the form without be directed to the ckeck.php and without placing the php code on top of the form in join.html

Is this possible? if not which other option do i have?

regards
adsegzy

To begin with validation of the form. If you change line 13 in your html file to

<input type="Button" value="Submit" onclick="final_check(this.form)">

You replace the submit option with a button that looks the same but will call some javascript.
The javascript can validate and if all ok submit the form to where you want it. Like so:

function final_check(form) {
    if ( form.name.value == "" ) {
        alert ("You didn't enter your name");
        return false;
    }
    if ( form.email.value == "" ) {
         alert ("You didn't enter an e-mail address");
        return false;
    }
    form.submit();
}

You could use more javascript to fully validate the email address.

Your PHP code (check.php) isn't needed anymore but anyway, it looks like valid code but will never work.
To get the values from the form you need to use something like this:

$name = isset($_POST['name']) ? $_POST['name'] : null;

This will check if name is there and $name will be the name entered in the form and if nothing was entered, $name will be "null".

Member Avatar for rajarajan2017

you can write both within a single php file. Just merge that into one single file and you can also call the same page from there itself.

you can write both within a single php file. Just merge that into one single file and you can also call the same page from there itself.

if i put the php code on the top of the form, how do i do it that people viewing my source code will not see/get the correct code of my php (for hackers sake)?

The PHP code is parsed on the server and only sends the output to the browser, not the whole piece of code (unlike JavaScript which is parsed by the client).

Member Avatar for rajarajan2017

Php code will not shown in the view source of html page, it get only parsed.

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.