-1

I have a webpage that automatically submit a value "val" in the input field whenever the page is loaded:

<form method="post" action="website.com/xxxx">
<input type="text" name="t" style="width:250px" value="val">
<button type="submit"><p>Submit<img src="submit.png" style="width:32px;vertical-align:middle"></p></button>
</form>
<SCRIPT LANGUAGE="JavaScript">document.forms[0].submit();</SCRIPT>

How do I make it automatically replace "val" with a non repeating variable chosen from a list(a txt file or within the html code)? Like for example I want to submit val1, val2, val3 and so on.. Everytime the page is loaded it choose one value from that list and then keep reloading until it submitted all the values without repeating any value. Also, how would I make it retrieve the values from an external source like a txt file? How would I also embed the values into the html code so it wouldn't need to retrieve the values from a file?


Edit: Ok I found out that "due to security restrictions, client-side scripts may not be allowed to access the user's computer beyond the web browser application" so I want to just embed the list of values into the html code.

jenny
  • 11
  • 3
  • You can [submit your form by AJAX](http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form) to keep your list, and the rest is easy. – iForests Dec 13 '13 at 17:40
  • @iForests Hi, could you post a sample code that makes this task? I have limited knowledge with html and javascript. And its the first time I heard of AJAX – jenny Dec 13 '13 at 17:48

2 Answers2

0

If you use jQuery, this will do (in a script tag):

$("form > input[name='t']").val("something-"+Math.random());

If you need sequential numbering, consider using cookies. Also, if you want to make sure no value will occur twice, you can use the current millisecond:

function msec() {var w=new Date();var n=w.getTime();return n;}
$("form > input[name='t']").val("something-"+msec());

Hope this helps.

dkellner
  • 5,301
  • 1
  • 31
  • 36
0

First of all, include jQuery between <head> and </head>.

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

And declare your list.

arr = ['val1', 'val2', 'val3', 'val4', 'val5'];

Now, when the form is submitted, we submit it by AJAX ($.post(...)).

$('form').on('submit', function( event ) {
    event.preventDefault();
    $('#t').val(arr.pop());
    $.post('xxxx.php', $(this).serialize());
});

You can see that the value send to the server is val5, val4, ..., val1.

Try here: http://jsfiddle.net/4McNm/

iForests
  • 6,013
  • 9
  • 34
  • 70