Jonathan Avfc Jones
Jonathan Avfc Jones

Reputation: 1

getting HTML to send content to PHP

Okay so I'm confused and lost and have no idea of what I am doing, basically here is my code:

HTML/JavaScript:

<img src="Images/Question1.png" usemap="#mainMap" id="main"/>
 <map name="mainMap" id="mainMap">
  <area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" />
 </map
        function incorrectAnswer() {
        document.getElementById("incorrectVid").style.display="block";
        document.getElementById("incorrectVid").play();
        document.getElementById("answer_id").value=id;
        document.forms["answer_send"].submit();
    }

<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
    <input type="hidden" id="answer_id" name="answer_id" value="10"/>
    <input type="submit" value="Submit" />
</form>

PHP:

$id=$_POST['answer_id'];
echo $id

I have no idea why this is not working any ideas/help?

Upvotes: 0

Views: 102

Answers (2)

To grab values submitted via GET/POST you need to assign the name property to your input fields. Try the below:

The HTML and JS

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script>
            function incorrectAnswer() {
                document.forms["answer_send"].submit();
            }
        </script>
    </head>
    <body>
        <img src="none.jpg" alt="placeholder" width="200" height="200" onClick="incorrectAnswer();">
        <form id="answer_send" name="answer_send" action="form.php" method="POST">
            <input type="hidden" id="answer_id" name="answer_id" value="10"/>
        </form>
    </body>
</html>

The PHP

<?php
    $id=$_POST['answer_id'];
    echo $id;
?>

Note: Your AREA element is not going to work unless you assign it to something. Your JavaScript function must be within tags or you'll receive errors. The HTML/PHP part of this is working fine... You are experiencing these issues due to the following three lines...

document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;

Upvotes: 2

Michael Antonius
Michael Antonius

Reputation: 972

where is your name attribute ?

Every input must have a name attribute to retrieve it on php and your html code must look like this:

<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/> </form>

Upvotes: 0

Related Questions