hayonj
hayonj

Reputation: 1449

PHP submit form if conditions are met

I'm NOT using javascript has it is disabled in my environment. Please review the code below. I'm trying to use PHP to create the logic that Javascript or jQuery would allow me to do with a simple : document.form2.submit()

<div>
    <h2>How many services do you need ?</h2>
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST" name="fServ">
    <input type="number" name="numServ" />
    <input type="submit" value="SEND">
    </form>
<div>
    <?php

    if(isset($_POST['numServ']) && $_POST['numServ']!==""){
        echo "We need ".$_POST['numServ']." services";
        echo "<form name='form2' id='form2' method='get' action= $_SERVER[PHP_SELF]>";
        for($k = 0 ; $k< $_POST['numServ']; $k++){
        //echo "<input type=\"text\" value=\"services$k\">";}
            echo "<br><input type='text' name='services$k' value=''>";

            if(empty($_GET['services'.$k])){
            echo "You didn't fill up all the fields. Please put in a service";
            }
                if (!empty($_GET['services'.$k])){
                echo "Form Filled";//
                }
            }   
    }
    echo "<input type='submit' name='sendForm' value='SEND'/></form>";



    if(!isset($_POST['numServ'])){
        echo "We don't have any services yet.";
    }
    else if($_POST['numServ']==""){
        echo "Please put in a number";
    }

    ?>

Can this be achievied through PHP ? Where if the conditions are met and the form fields are NOT blank then submit the form, otherwise die and show a message.

Upvotes: 0

Views: 1261

Answers (2)

Santi
Santi

Reputation: 16

If I understand, you need auto-submit the form without javascript. Maybe this thread helps you: How to submit a form on page load without clicking submit button?

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Even then you need to submit the details of the form:

Name: <input type="text" name="name" /> (Min 5 Chars)
Age: <input type="text" name="age" /> (Should be a Number)
<input type="submit" />

Now using server-side validation, this can be done this way:

if (isset($_POST["name"], $_POST["age"]))
  if (strlen($_POST["name"]) > 5 && is_numeric($_POST["name"]))
    die("Error! The conditions are not met!");
die("Form Submitted!");

This way you can submit the form where JavaScript is disabled.

Upvotes: 1

Related Questions