Jump to content


Photo

Getting Html To Send Content To Php


  • Please log in to reply
3 replies to this topic

#1 JonathanAvfcJones

JonathanAvfcJones

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 10 December 2012 - 07:11 AM


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?


#2 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 10 December 2012 - 10:56 AM

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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#3 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 10 December 2012 - 08:29 PM

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, 10 December 2012 - 08:30 PM.


#4 JonathanAvfcJones

JonathanAvfcJones

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 12 December 2012 - 07:20 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com