Jump to content

Getting Html To Send Content To Php


JonathanAvfcJones

Recommended Posts

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

HTML/Javascript:

onclick="incorrectAnswer()

 

function incorrectAnswer() {

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

when i done this in the answersend.php page as it loads on the server the page is blank, I have no idea why this is not working any ideas/help?

Link to comment
Share on other sites

Your repost of this in the php forum section has been removed.

 

Where exactly is the onclick= ... statement in the HTML on your page, because you cannot just put it in the middle of nowhere. It needs to be inside the HTML tag of something that can be clicked on - i.e. show us the actual code that reproduces the problem that you need help with.

Link to comment
Share on other sites

If you want your javascript function to run when they submit the form, you have to add the onclick event handler to the submit button like so:

 

<input type="submit" value="Submit" onclick="incorrectAnswer()" />

 

When you submit your form, this function will run first. You can return true or false from your javascript function to determine whether to carry on with the submit, or whether to cancel the submit.

 

Let's say your function validates some fields, if the user entered the wrong data, you can return false from the function which will stop the submission to the server. You accommodate this by changing the input tag to this:

 

<input type="submit" value="Submit" onclick="return incorrectAnswer()" />

 

You return true or false depending on what the results of your 'test' was. False stops the submission, true carries on and executes the php script. If you're having some difficulty, i'd drop the javascript and validate it all with the php.

 

In relation to your value not being echo'd, it might seem quite a silly thing to ask, but you have added the closing semicolon haven't you? It's just in your example there is no semicolon after the echo statement.

 

It's hard to say what it could be. Are you getting any error messages come up? PHP usually tells you exactly where the error is, which is a blessing.

 

Kind regards,

 

AotB.

Edited by AoTBuNgLe
Link to comment
Share on other sites

Your repost of this in the php forum section has been removed.

 

Where exactly is the onclick= ... statement in the HTML on your page, because you cannot just put it in the middle of nowhere. It needs to be inside the HTML tag of something that can be clicked on - i.e. show us the actual code that reproduces the problem that you need help with.

 

sorry about that i have been away for a few days this is what i have :

 

html/javascript

 

 

 

<img src="Images/Question1.png" usemap="#mainMap" id="main" style="position:absolute;display:none;left:0px;top:0px;" border="0" />

<map name="mainMap" id="mainMap">

<area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" />

</map>

 

function incorrectAnswer() {

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.