2

I'm trying to create a form without a real submit button, but with a styled a href and javascript, but I'm unable to POST the form itself.

If I do var_dump($_POST); then I get an array of 4 items (3 inputs and 1 text area). The form itself is not there, so if (isset($_POST['submit'])) valuates to false. I also tried if (isset($_POST['ContactForm'])) but its not working either

form code:

<form name='ContactForm' id='ContactForm' action='verzendvraag.php' method='POST'>
    <input type="text" class="col-md-6 col-xs-12 name" id="naam" name='naam' placeholder='Naam *' value="<? echo $_SESSION['naam']; ?>" required/>
    <input type="email" class="col-md-6 col-xs-12 Email" id="email" name='email' placeholder='Email *' value="<? echo $_SESSION['email']; ?>" required/>
    <input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' id='Subject' placeholder='Onderwerp'/>
    <textarea type="text" class="col-md-12 col-xs-12 Message" id="vraag" name="vraag" placeholder='Bericht *' style="left: 0px; top: 0px" required><? echo $_SESSION['vraag']; ?></textarea>
    <div class="cBtn col-xs-12">
        <ul>                                
            <li class="clear"><a href="#" onclick="resetForm(event)"><i class="fa fa-times"></i>ledig vakjes</a></li>
            <li class="send"><a href="#" onclick="submitForm(event)"><i class="fa fa-share"></i>Verstuur bericht</a></li>
        </ul>
    </div>
</form>

javascript:

function submitForm(e) {
    if (validateForm())
    {
        document.getElementById("ContactForm").submit();
    }
    e.preventDefault();
}

php:

session_start();    
//Validatie serverside => bescherming inbouwen voor als iemand zijn javascript uitschakelt. Javascript echter wel nodig om de server en netwerktraffic te ontlasten. 
if (isset($_POST['submit']))
{ 
    $naam  = $_POST['naam'];
    //$bedrijf = $_POST['bedrijf'];
    $subject = $_POST['Subject'];
    $email = $_POST['email'];
    $vraag = $_POST['vraag'];

    //In session steken zodat we bij een eventuele terugkeer naar de form de reeds ingevulde gegevens kunnen terug zetten.
    $_SESSION['naam']  = $_POST['naam'];
    //$_SESSION['bedrijf'] = $_POST['bedrijf'];
    $_SESSION['Subject'] = $_POST['Subject'];
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['vraag'] = $_POST['vraag'];

    $isOK = true;
    $error = '0';

    $atpos = strrpos($email,"@");
    $dotpos = strrpos($email,".");

    if(is_null($naam) || $naam == '')
    {
        $isOK = false;
        echo('NAAM NIET OK ');
        $error .= ',1';
    }

    if(is_null($email) || $email == '')
    {
        $isOK = false;
        echo('EMAIL NIET OK '.$email);
        $error .= ',2';
    }
    else
    {
        //checken geldig e-mailadres
        if ($atpos === false || $dotpos === false || $dotpos < $atpos+2 || $dotpos+2 >= strlen($email))  //=== moet => http://php.net/manual/en/function.strpos.php en http://www.php.net/manual/en/language.operators.comparison.php
        {
            $isOK = false;
            echo("Ongeldig e-mailadres ");
            $error .= ',3';
        }
    }

    if(is_null($vraag) || $vraag== '')
    {
        $isOK = false;
        echo('VRAAG NIET OK ');
        $error .= ',4';
    }
}

if(!($isOK))
{
    echo("<script>location.href='index.php?error=$error'</script>");
    exit;
}
else
{
.....
}

I checked Javascript Submit is not Sending POST to php file and he says his code works, so I don't understand mine won't.

8
  • 1
    "without a real submit button, but with a styled a href and javascript" — Don't do that. Style the submit button instead. Commented Dec 29, 2014 at 11:25
  • You don't have a named element called "submit". So, based on the conditional you've set for it, is the reason it's failing, or a contributing one at best. Commented Dec 29, 2014 at 11:26
  • "I checked Javascript Submit is not Sending POST to php file and he says his code works, so I don't understand mine won't." - He has his conditional if(isset($_POST['form'])) based on his form id <form id="form" and onclick="document.getElementById('form').submit(); - Plus, make sure short tags are enabled. Commented Dec 29, 2014 at 11:33
  • I already tried if (isset($_POST['ContactForm'])) @Fred -ii- Commented Dec 29, 2014 at 11:50
  • 1
    I'll check both name and e-mail, thanks for helping out! Commented Dec 29, 2014 at 12:53

4 Answers 4

1

Since you don't have any button or input named submit so it does not exist in $_POST variable. I think you should check what you have in the form so use .

if (isset($_POST['naam'])){
// your code 
}  

Instead of

if (isset($_POST['submit'])){
// your code 
}
Sign up to request clarification or add additional context in comments.

Comments

1

you should also change this if (isset($_POST['submit'])) to if (isset($_POST['naam']))

2 Comments

This is a possibility, but will I be sure the user did submit?
If user submit then you using Ajax to submit form
0

Use ajax request like this

$.ajax({//create an ajax request to load_page.php
        type: "POST",
        url: "receive.php",
        data: $("#ContactForm").serialize(),
        success: function(response) {
            if (response) {
                alert('Successfully posted.');
            }
            else {
                alert('Successfully not posted.');
            }
        }
    });

Comments

0

Just use ajax for submiting form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

<body>
<form name='ContactForm' id='ContactForm' action='verzendvraag.php' method='POST'>
<input type="text" class="col-md-6 col-xs-12 name" id="naam" name='naam' placeholder='Naam *' value="<? echo $_SESSION['naam']; ?>" required/>
<input type="email" class="col-md-6 col-xs-12 Email" id="email" name='email' placeholder='Email *' value="<? echo $_SESSION['email']; ?>" required/>
<input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' id='Subject' placeholder='Onderwerp'/>
<textarea type="text" class="col-md-12 col-xs-12 Message" id="vraag" name="vraag" placeholder='Bericht *' style="left: 0px; top: 0px" required><? echo $_SESSION['vraag']; ?></textarea>
            <div class="cBtn col-xs-12">
                <ul>                                
                    <li class="clear"><a href="#" onclick="resetForm(event)"><i class="fa fa-times"></i>ledig vakjes</a></li>
                    <li class="send"><a href="#" onclick="submitForm(event)"><i class="fa fa-share"></i>Verstuur bericht</a></li>
                </ul>
            </div>
        </form>
        <script>        
        function submitForm(e) {
        alert('working');
        var naam = $("#naam").val();
        var email = $("#email").val();
        var Subject = $("#Subject").val();

        $.ajax({//create an ajax request to load_page.php
        type: "POST",
       url: "verzendvraag.php",
       data: {"naam":naam,"email":email,"Subject":Subject},
       success: function(response) {
        if (response) {
        alert(response);
            alert('Successfully posted.');
        }
        else {
            alert('Successfully not posted.');
        }
    }
});
}
</script>

</body>
</html>

verzendvraag.php

 <?php session_start();    
  //Validatie serverside => bescherming inbouwen voor als iemand zijn javascript uitschakelt. Javascript echter wel nodig om de server en netwerktraffic te ontlasten. 


  if (isset($_POST))
   { 
    $naam  = $_POST['naam'];
  //$bedrijf = $_POST['bedrijf'];
   $subject = $_POST['Subject'];
   $email = $_POST['email'];
  //$vraag = $_POST['vraag'];

  //In session steken zodat we bij een eventuele terugkeer naar de form de reeds ingevulde gegevens kunnen terug zetten.
$_SESSION['naam']  = $_POST['naam'];
//$_SESSION['bedrijf'] = $_POST['bedrijf'];
$_SESSION['Subject'] = $_POST['Subject'];
$_SESSION['email'] = $_POST['email'];
//$_SESSION['vraag'] = $_POST['vraag'];

$isOK = true;
$error = '0';

$atpos = strrpos($email,"@");
$dotpos = strrpos($email,".");

if(is_null($naam) || $naam == '')
{
    $isOK = false;
    echo('NAAM NIET OK ');
    $error .= ',1';
}

if(is_null($email) || $email == '')
{
    $isOK = false;
    echo('EMAIL NIET OK '.$email);
    $error .= ',2';
}
else
{
    //checken geldig e-mailadres
    if ($atpos === false || $dotpos === false || $dotpos < $atpos+2 || $dotpos+2 >= strlen($email))  //=== moet => http://php.net/manual/en/function.strpos.php en http://www.php.net/manual/en/language.operators.comparison.php
    {
        $isOK = false;
        echo("Ongeldig e-mailadres ");
        $error .= ',3';
    }
}

   /* if(is_null($vraag) || $vraag== '')
{
    $isOK = false;
    echo('VRAAG NIET OK ');
    $error .= ',4';
}*/
}

if(!($isOK))
 {
echo("<script>location.href='index.php?error=$error'</script>");
exit;
}
else
{
echo "SUCCESS";
}
 ?>

3 Comments

I don't think AJAX will solve my problem, if it doens't know $_POST['submit'] or $_POST['ContactForm'] now, it won't know with AJAX either.
so instead "if (isset($_POST['submit']))" use "if (isset($_POST))"
please try now!!!write my first code in index.php and for second code,create verzendvraag.php and paste that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.