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.

Community
  • 1
  • 1
diedie2
  • 366
  • 1
  • 6
  • 17
  • 1
    "without a real submit button, but with a styled a href and javascript" — Don't do that. Style the submit button instead. – Quentin Dec 29 '14 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. – Funk Forty Niner Dec 29 '14 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 `
    – Funk Forty Niner Dec 29 '14 at 11:33
  • I already tried `if (isset($_POST['ContactForm']))` @Fred -ii- – diedie2 Dec 29 '14 at 11:50
  • why dont you try if(isset($_POST['naam'])) instead of if(isset($_POST['ContactForm'])) – Faruk Omar Dec 29 '14 at 12:10
  • Because in that case I'm not sure the user pressed 'submit' (or am I wrong thinking that?) – diedie2 Dec 29 '14 at 12:14
  • No if user click the link then you submit the form via javascript so this is same as click submit button. I think you no need to worry about that. – Faruk Omar Dec 29 '14 at 12:24
  • 1
    I'll check both name and e-mail, thanks for helping out! – diedie2 Dec 29 '14 at 12:53

4 Answers4

1

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

user3111011
  • 145
  • 1
  • 9
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 
}
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22
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.');
            }
        }
    });
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";
}
 ?>
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • 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. – diedie2 Dec 29 '14 at 12:05
  • so instead "if (isset($_POST['submit']))" use "if (isset($_POST))" – Priyank Dec 29 '14 at 12:21
  • please try now!!!write my first code in index.php and for second code,create verzendvraag.php and paste that code. – Priyank Dec 29 '14 at 12:30