0

I have several forms on my single PHP page. All look like this:

    <form id="test_form_1" action="test_submit.php" method="post" name="test_form">
    <label>This is Question #1:</label>
    <p>
      <label>
        <input type="radio" name="Question1" value="ans1" id="Question1_0">
        Answer1</label>
      <br>
      <label>
        <input type="radio" name="Question1" value="ans2" id="Question1_1">
        Answer2</label>
      <br>
 <button type="submit" name="btnSubmit" id="btnSubmit">Submit</button>
    </form>

But ID of each form is different, like test_form_1, test_form_2, test_form_3, and so on. I am calling these forms via HEREDOCS once PHP decide which ID the user belongs to.

Then I am using a JavaScript to automatically submit the form after 5 seconds.

<script>
window.setTimeout(function() {
    document.getElementById('test_form_1').submit();
}, 5000);
</script>

Here are the HEREDOCS:

$test1= <<<HTML
<table>Month 1 Test.</table>
HTML;
$test2= <<<HTML
<table>Month 2 Test.</table>
HTML;
//and so on...

And here is the PHP part:

<?php
if($user_category == test1){
echo"test1";
}
if($user_category == test2){
echo"test2";
}

My question: Is it possible to pass IDto getElementById of that form only to which the user actually belongs? So that it only submits that form to the database to which the user actually belongs. Or is there any better alternative to achieve this?

user2561597
  • 11
  • 2
  • 5

4 Answers4

1

Try this :

<script>
  var userID = "<?php echo $userID ?>"; // For example

  window.setTimeout(function() {
    document.getElementById('test_form_'+ userID).submit();
  }, 5000);
</script>
Pouki
  • 1,654
  • 12
  • 18
1

I don't know your PHP code, but you can do something like this (assuming that the id lives in $ID)

<script>
window.setTimeout(function() {
    document.getElementById('test_form_<?=$ID;?>').submit();
}, 5000);
</script>
trizz
  • 1,447
  • 12
  • 27
  • This should be an obvious, but for some it isn't. Do note that if you put this in an external javascript file it will NOT parse the file as PHP, and hence you'll end up with a litteral "id" of `test_form_=$ID;?>`. If you want to use it in an external JS file, refer to a variable instead, and set it with a script before you load your external JS file. – h2ooooooo Jul 09 '13 at 09:21
  • keep the PHP tags out of the quotes or else it would become a string! – Mohammad Areeb Siddiqui Jul 09 '13 at 09:24
  • 1
    @MohammadAreebSiddiqui Nope - PHP would parse the document before the browser even could get near it, and PHP doesn't give a damn about anything that isn't in PHP tags. The browser would get this served as `document.getElementById('test_form_2').submit()`. – h2ooooooo Jul 09 '13 at 09:25
  • @MohammadAreebSiddiqui I guess that's the point of the question. It has to become, for example, `test_form_1`. – trizz Jul 09 '13 at 09:26
  • NO! @trizz the tags "" will also be counted as a string not as a tag :) – Mohammad Areeb Siddiqui Jul 09 '13 at 09:27
  • 1
    @MohammadAreebSiddiqui No offense, but do you have any PHP experience? Like h2ooooooo mentioned earlier, this will not work if you use an external JS file. It **will** work perfectly fine if you put this in a PHP page! See h2ooooooo comment above. – trizz Jul 09 '13 at 09:30
  • @trizz I made this site using PHP I know it's not that cool but still: www.simpleblog.comlu.com – Mohammad Areeb Siddiqui Jul 09 '13 at 09:31
1

If your user_id is stored in session then you could do it like this:

<script>
  <?php session_start(); ?>
  var id= <?php echo $_SESSION['user_id']; ?>

  window.setTimeout(function() {
    document.getElementById('test_form_'+ id).submit();
  }, 5000);
</script>
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
0

Is the userID connected to the formID? Wouldn't it be easier to keep track of which user it is, by a session variable, in the PHP script? This way, you cannot cheat your way in the script, and vote for someone else.

Because if you keep the userID in a variable in the script, one could easily change this to w/e he/she wants to. And in turn, vote for any user he/she wants to.

So when a user logs in, do something like this:

<?php
session_start();
$_SESSION['sess_userID'] = $userID_of_the_loggedin_user;
?>

Then in your remote-script-file, test_submit.php:

<?php
session_start();
// Grab $_POST data and such
// Then perform something with $_SESSION['sess_userID'];
// Like SQL; "UPDATE votes SET answer=10 WHERE userID=$_SESSION['sess_userID']"
// But have in mind the SQL-injection.
?>
Eric
  • 18,532
  • 2
  • 34
  • 39