Javacadabra
Javacadabra

Reputation: 5758

Form not submitting values to POST array PHP

I have a form which can be seen below:

<form id="myForm" action="register.php" method="POST">
    <input type="text" name="email" id="email" onfocus="fieldSwap('inputhover.png')" 
        onblur="fieldSwap('inputnorm.png')"><br/>
    <input type="image" src="submitnorm.png" name="submit" id="submit" 
        alt="submit button" onMouseOver="buttonSwap('submithover.png')" 
        onMouseOut="buttonSwap('submitnorm.png')" 
        onMouseDown="buttonSwap('submitclick.png')" 
        onMouseUp="buttonSwap('submitnorm.png')"/>
</form>

It calls this script:

<?php
    require_once 'db.php';

    $email = $_POST['email'];
    echo $email;
    echo "Register form";
    $sql->$db->prepare("INSERT INTO emails SET email = :email");
    $sql -> bindValue(':email', $email, PDO::PARAM_STR);
    $sql -> execute();

    // Redirect back to homepage
    header('Location: index.php');
    exit();
?>

I am getting this error:

Notice: Undefined index: email in C:\wamp\www\Holding page\register.php on line 3

What am I doing wrong? I understand that no index called email exists in the POST array but I am unsure why this is the case. Thanks!

Upvotes: 2

Views: 7236

Answers (2)

Karl Lorey
Karl Lorey

Reputation: 1616

Try submitting your form via

<input type="submit" value="send">

and see if that works with `print_r().

Sadly, I don't know why <input type="image"> is not working for you. Try checking the doctype (must be HTML5) and your browser (too old?).

Upvotes: 2

sAnS
sAnS

Reputation: 1163

you forget to add value attribute in email as well as if input type=image not working for submit button then you may try type=submit

<form id="myForm" action="register.php" method="POST">
<input type="text" name="email" id="email" value="" onfocus="fieldSwap('inputhover.png')" onblur="fieldSwap('inputnorm.png')"><br/>
<input type="submit" src="submitnorm.png" name="submit" id="submit" alt="submit button" onMouseOver="buttonSwap('submithover.png')" onMouseOut="buttonSwap('submitnorm.png')" onMouseDown="buttonSwap('submitclick.png')" onMouseUp="buttonSwap('submitnorm.png')"/>
</form>

Upvotes: 0

Related Questions