Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS variable passed to html form

    I need to pass javascript variables "a1" to "a35" to respective "hidden" form inputs.

    I have up to 35 students in a class. Rather than have the page reload (and deal with potential slow downs each time during class) I decided to write a program which allows me to edit each student's variable by +1 or -1 as many times as I like ... and THEN submit the form for ONE php database update upon completion. I know how to do everything except pass the javascript variables by form to the form handler.

    I tried the following:
    Code:
    	$count = 0;
    	while ($count <= $num_rows){
    		print "<input type=\"hidden\" name=\"stu".$count."\" value=\"a".$count."\"><br>";
    		$count++;
    	}
    $num_rows = the number of students in the class (roughly 35);
    I knew the coding wouldn't work, but it was what my previous experience with forms led me to believe was a good start.


    The actual javascript variable values are set by the following code:
    Code:
    $count = 0;
    while ($count <= $num_rows){
    	print "<script type=\"text/javascript\">";
    	print "var a$count=0;";
    	print "function increase$count()";
    	print "{a$count++;";
    	print "document.getElementById('boldstuff$count').innerHTML= +a$count;}";
    	print "</script>";
    
    	$count++;
    }
    ... and

    Code:
    	$count = 0;
    	$query1 = "SELECT * FROM chs_students WHERE spanish = '$period' and uname != 'admiralq' ORDER BY lname, fname";
    	$result1= mysql_query($query1) or die("Could not perform query: ".mysql_error());
    	while ($row1 = mysql_fetch_array($result1)){
    		$count++;
    		print $count.". <span onclick=\"increase".$count."();\">".$row1['lname'].", ".$row1['fname']."<b id=\"boldstuff".$count."\">0</b></span>&nbsp;&nbsp;".$a1."&nbsp;&nbsp;<span onclick=\"decrease".$count."();\">(minus)</span><br>\n";
    	}
    God, this is a mess, isn't it? Thanks.

  • #2
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a link to the actual test page.

    http://pcsspanish.com/js_test.php?period=1

    If you click the name, the variable increments. If you click "minus", it decreases. After I set all the variables for what I want, I want to submit them through an html form.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You need to realize that PHP is strictly a server side language and Javascript (at least in your case) is strictly a client side language.

    Once PHP finished to create the initial page on the server, it outputs the result as HTML+Javascript to the browser. The browser then interprets the HTML and executes the Javascript.

    So if you want to move the values into hidden fields inside the browser, you'll have to use Javascript ... PHP is not possible here. You can use the PHP code you propose above to initially create the hidden inputs. It would help to add id attributes to them though.

    But one more important thing: As soon as you start to create variables (and field names) like a1, a2, a3, ...., aHIGHNUMBER you already did something wrong. This is exactly the domain for arrays (like a[1], a[2]). So you have ONE variable instead of an undefined number. You can easily use indexes and the .length property to traverse the array.

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    You need to realize that PHP is strictly a server side language and Javascript (at least in your case) is strictly a client side language.
    I understand that. I'm inexperienced in javascript, not PHP. That is why I need to put the variable value in an HTML form. I'm using php simply to write the html input code in a loop.

    But one more important thing: As soon as you start to create variables (and field names) like a1, a2, a3, ...., aHIGHNUMBER you already did something wrong.
    And I understand what you said about arrays. But I know little about javascript, and so when I found a slice of code that performed similarly to what I need, I modified it to function properly for ONE student ... and then once tested well, I used a PHP WHILE loop again to write the javascript code. I understand that you don't like the variable names. Neither do I, but like I said, I know little about javascript, and so EVEN LESS about ARRAYS in javascript. My javascript experience consists of finding what other people have written, and then making small changes to fit my needs. So, was it WRONG? No, it works. It's not efficient. It's not pretty. It's not even good programming. But I'm inexperienced in javascript, don't know how to do arrays and, most importantly, it works.

    I appreciate your input, but I've got a fully functional page here except for one thing ... I need to pass the javascript variable to a simple html input field. The javascript variable is "a1." I thought this would be something simple like:

    Code:
    <input type="hidden" name="a1" value="document.write(a1)">
    But, like I said. i don't really know what I'm doing with javascript.

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Code:
    <input type="hidden" name="a1" id="a1" value="">
    Code:
    document.getElementById('a1').value = a1;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •