JonathanAvfcJones Posted December 10, 2012 Share Posted December 10, 2012 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? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2012 Share Posted December 10, 2012 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. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted December 11, 2012 Share Posted December 11, 2012 (edited) 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 December 11, 2012 by AoTBuNgLe Quote Link to comment Share on other sites More sharing options...
JonathanAvfcJones Posted December 12, 2012 Author Share Posted December 12, 2012 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.