Well Javascript is new to me but I didn’t think it would cause a problem like this. Either I’m doing this wrong or I should be using cookies instead.
I’m trying to use javascript to submit POST data to a php page via a link. I asked a question earlier about how to pass data like this, and besides AJAX it basically came down to making a hidden form and having javascript submit it. I don’t know anything about AJAX or Jquery right now so I’m sticking with the form method.
BUT for some reason the code wasn’t posting my data. I couldn’t figure out why until I accidentally clicked it twice in a row, and then the data showed up.
The scenario is the link takes you to the page where the data goes, and the data gets processed and shown via var_dump.
Here’s necessary coding.
function red()
{
alert('This is happening');
document.traf.traf2.value = 3;
document.traf.submit();
}
and
echo "<form name='traf' method='post' action='newphpwriterthing.php'><input type='hidden' name='traf2' /></form>"; //FORM SET
echo "<a href='newphpwriterthing.php' onClick='red()'> <img src='red.png' height='150' /></a>";
The function seems to be called fine, but I was under the impression that the value and submitting wasn’t doing anything…until I hit it twice on accident and it did exactly what it should.
I also tried clicking once to get to the page, and then refreshing multiple times to see if the value just “wasn’t there yet”. Nothing.
What’s going on here and how do I fix it?? Am I better off using cookies?
You link have a href so it’s just getting you to that URL.
Add
return false;to the end of yourredfunction.Or maybe you should chage
onClick='red()'toonClick='red() return false;'– didn’t used it that way for some time.