-1

My problem is basically that i have something like this:

if (!empty($_POST['field1'])OR !empty($_POST['field2']) OR !empty($_POST['field3'] ))
{
// SUBMIT FORM 
} else {    
// display error message 
}

My problem is that i dont really know how to submit a form using code since im not really experienced in PHP.

I'm using all the !empty($_POST['field1'] since i have 16 input fields in my form and the first 5 are ALWAYS required but in the other 11 input fields a minimal of 1 input is required, i thought this was the most simple solution.

A little question on the side, i'd also like to know how to stop a form from submitting to another page so i can write an error or something on the page it was.

Déjà vu
  • 774
  • 2
  • 9
  • 31
  • 1
    As ever, you need to post what you have already tried... – charliefortune Jan 21 '14 at 08:45
  • PHP is not what you need here. PHP composes pages that are sent to the browser. You want to control the browser posting back to the server. For that you'll need some javascript (or jQuery). – paul Jan 21 '14 at 08:58

5 Answers5

0

you can check the fields only on submitting the form in php. so just submit the form to the same page, on the top of the page before html starts, add an if condition to satisfy the conditions you need. if the condition is false the same page could be loaded back with a variable which has your error message. additionally you can use javascript for client side validation.

Joe
  • 618
  • 1
  • 9
  • 18
0

Do all the validations on the client side and if everything is good then submit the form from javascript or else display the error on same page...

document.getElementById('theForm').submit();

Refer to below link for more insight:

How to submit a form using javascript?
How to submit a form with JavaScript by clicking a link?

Server side validations can also be done in PHP, in that case you need to redirect to the page that contains the form via PHP as given here http://in2.php.net/manual./en/function.header.php

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112
0

You can not submit a form with PHP, you need to use Javascript for that (or jQuery).

With Javascript:

document.getElementById("your_form_name").submit();

or with jQuery:

$('#your_form_name').submit();

Per your 2nd question - when you define the form, you define the action property.

Normally, 'script.php' will be a separate file and that will cause the browser to navigate to this file. To bypass this, you have at least 2 options:

  1. Change 'action' to the same file your form is located at. Basically you will make the script post to itself.
  2. Use Ajax to post the form data. Ajax will work in the background and you will be able to dynmically update the page, without the need to refresh. Take a look at jQuery Ajax Submit Form for more info.

Hope this helps!

Community
  • 1
  • 1
dev7
  • 6,259
  • 6
  • 32
  • 65
0

Why do you want to submit the variables a second time? your requests !empty($_POST['field1']) etc. seems like you already submitted the variables and are now checking them if they are empty or not...

if so, you could set a "flag" whether your variables are OK or not. so for example:

if (!empty($_POST['field1'])OR !empty($_POST['field2']) OR !empty($_POST['field3'] ))
//or other if condition
{
  $countinue = true; // set "flag"
}
else
{
  $continue = false;
}

and then:

if($continue == true)
{
// continue what you want to do next
}
else
{
// stop process and echo error message
}

another possibility could be to submit your variables again using the GET-method after checking them. This means calling another page and adding your variables (encrypted) to the URL, for example instead of setting your flag-variable $continue = true:

header("Location:next-page.php?var1=$_POST['field1']&var2=$_POST['field2]");
SaschaP
  • 883
  • 1
  • 7
  • 25
0

you can do like this

    if(isset($_POST['field1'],$_POST['feild2'],$_POST['field3']))
    {
        // submit the form
    }

for showing the error message you can use javascript or jQuery here I'm using jquery.

  1. include jquery in your html head.
  2. now write this script in a separate file. and add the .js in your head part.
    $(document).ready(function(){
        var val1 = $("#IdOfField1").val();
        var val2 = $("#IdOfField2").val();
        var val3 = $("#IdOfField3").val();
        // now validate the fields as per your requirement.
        if(val1!='' || val1.lengh != 0)
            { alert('Enter your name');
             return false;}
        else{return true;}
    });
Kaushik
  • 2,072
  • 1
  • 23
  • 31