Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2010, 11:15 PM   PM User | #1
capt_nemo777
New Coder

 
Join Date: Jul 2008
Posts: 63
Thanks: 12
Thanked 0 Times in 0 Posts
capt_nemo777 is an unknown quantity at this point
Question PHP variable in javascript ?

Hi,

is it cool/possible to use a php variable inside the javascript ?
i have this hidden html form field
PHP Code:
<?php $unique md5(uniqid()); ?>
<input type="hidden" name="unique" id="<?php echo $unique?>" value="" />
then i have this javascript
PHP Code:
var hiddenfield = document.getElementById('<?php echo $unique?>');
hiddenfield.value = new Date().getTime();
LOL it doesn't work, can somebody help me please ?
capt_nemo777 is offline   Reply With Quote
Old 02-15-2010, 11:21 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
That will work--though it's a bit of overkill--*IF* you create the PHP variable at the very top of your PHP page, so it has the same value (and is available when needed) throughout the entire page.

Re a bit of overkill: Unless you have multiple <input> fields all named "unique", you can get a reference to that field by just doing
Code:
    var hiddenfield = document.YourFormName.unique;
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-15-2010, 11:28 PM   PM User | #3
capt_nemo777
New Coder

 
Join Date: Jul 2008
Posts: 63
Thanks: 12
Thanked 0 Times in 0 Posts
capt_nemo777 is an unknown quantity at this point
Question

Quote:
Originally Posted by Old Pedant View Post
That will work--though it's a bit of overkill--*IF* you create the PHP variable at the very top of your PHP page, so it has the same value (and is available when needed) throughout the entire page.

Re a bit of overkill: Unless you have multiple <input> fields all named "unique", you can get a reference to that field by just doing
Code:
    var hiddenfield = document.YourFormName.unique;
I have multiple rows that's why I have to generate unique id for each, and they were all named unique LOL, what should I do?, should I use a randomly generated php stuff and get it referenced via js like this ?
Code:
var hiddenfield = document.myFormName.<?php echo $unique; ?>
is that valid or ?

I also tried this
Code:
var hidden = document.myFormName."<?php echo $unique; ?>"
hidden.value  = new Date().getTime();
where $unique is md5(uniqid())
and Tadaa!!!..it didn't work lol

Last edited by capt_nemo777; 02-15-2010 at 11:42 PM..
capt_nemo777 is offline   Reply With Quote
Old 02-15-2010, 11:56 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm....but how does you JS code know WHICH ONE of those many <input>s it should be referring to???

Let's write this in pure HTML just to demo:
Code:
<input type="hidden" name="unique" id="x7312" value="" />
<input type="hidden" name="unique" id="ar541" value="" />
<input type="hidden" name="unique" id="p819" value="" />
<input type="hidden" name="unique" id="g713" value="" />
Okay, so now your JS code does:
Code:
var hidden = document.getElementById("??? what do you put here???");
See what I mean? WHICH ONE of those fields do you want to get in the JS code???

Incidentally, if this <form> is going to be sent to a subsequent PHP page, and if you want to be able to get the values of any and all of those hidden fields on that PHP page, then you *must* use
Code:
<input type="hidden" name="unique[]" id="..whatever.." value="" />
Same as when you use multiple checkboxes with the same name.

Not to ask a dumb *** question, but would it be adequate to do this???
Code:
<?php
$unique = 0;
while ( ... some loop creating all this stuff ... )
{
    ++$unique;
    ...

?>
    ...
    <input type="hidden" name="unique<?php echo unique;?>" value="" />
    ...
<?php 
}
?>
And now your JS code can do:
Code:
     var which = 7; // or any other number
     var hidden = document.FormName.elements["unique"+which];
    ...
And now, also, each of your form fields has a different name and you don't have to use [] to make them accessible in the subsequent PHP page.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
capt_nemo777 (02-16-2010)
Old 02-16-2010, 12:00 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh, by the way: You can't start an ID with a digit. If your hash function returns only numbers, then add a single letter prefix:
Code:
<input type="hidden" name="unique" id="U<?php echo $unique; ?>" value="" />
and then
Code:
var hiddenfield = document.getElementById('U<?php echo $unique; ?>');
That assume you decide to stick with your original scheme. Which I'm skeptical of for other reasons as noted above.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-16-2010, 12:44 AM   PM User | #6
capt_nemo777
New Coder

 
Join Date: Jul 2008
Posts: 63
Thanks: 12
Thanked 0 Times in 0 Posts
capt_nemo777 is an unknown quantity at this point
Question

I managed to make the id of each and every row unique through the while loop
but am still have trouble with the JS stuff when trying to set the value of each unique row

Code:
      var number = 100;
      var hiddenfield = document.test.name["unique"+ number];
      hiddenfield.value = new Date().getTime();
OR

Code:
      var number = 100;
      var hiddenfield = document.getElementById("unique" + number);
      hiddenfield.value = new Date().getTime()
capt_nemo777 is offline   Reply With Quote
Old 02-16-2010, 12:53 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No no no...

A name is *NOT* an ID!

So
Code:
      var number = 100;
      var hiddenfield = document.getElementById("unique" + number);
isn't even close. And name is *NOT* an indexable property of a form, so this
Code:
      var hiddenfield = document.test.name["unique"+ number];
makes no sense. (document.test.name would give you, what else, "test"--the name of the form named "test").

Try:
Code:
      var number = 100;
      var hiddenfield = document.test.elements["unique" + number];
With modern browsers, you can omit the elements[] as the implied collection of a <form> *is* the elements, so this should work as well:
Code:
      var number = 100;
      var hiddenfield = document.test["unique" + number];
Don't confuse names and ids; they aren't at all the same thing. [Except that in MSIE if you don't give an ID to a form field, then you can refer to it by ID and it promotes the name to be the ID. But please don't do this. It only work in MSIE in any case. And I think it *might* be fixed in MSIE 8.]
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-16-2010, 01:04 AM   PM User | #8
capt_nemo777
New Coder

 
Join Date: Jul 2008
Posts: 63
Thanks: 12
Thanked 0 Times in 0 Posts
capt_nemo777 is an unknown quantity at this point
sir,
i did all the tips you gave me, none worked
Code:
      var number = 100;
      var hiddenfield = document.test.elements["unique" + number];
      hiddenfield.value = new Date().getTime();
or even this
Code:
      var number = 100;
      var hiddenfield = document.test["unique" + number];
btw here's my php code
PHP Code:
    <?php echo form_open('task/startdate',array("name"=>"test")); ?>
    <?php $name 0; while($name sizeof($task->result())):?>
      <?php foreach($task->result() as $row): ?>
        <tr>
        <td><a title="<?php echo $row->descr?>" href="#"><?php echo $row->title?></a></td>
        <td><input type = "button" id =<?php echo $row->title?> value = "Unlocked" onmouseover = "asktolock(this.id)"  onclick = "lockme(this.id)"></td>
        <td><?php echo $row->assign_to?></td>
        <td><a id="txt"></a></td>
        <?php ++$name?>
        <?php $unique 'U'.md5(uniqid()).$row->id?>
        <input type="hidden" name="unique<?php echo $name?>" id="<?php echo $unique?>" value="" />
        </tr>
     <?php endforeach; ?>
     <?php endwhile; ?>
    <?php echo form_close(); ?>
capt_nemo777 is offline   Reply With Quote
Old 02-16-2010, 02:28 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the bug must be in your PHP code.

Here's an all HTML example:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script style="text/javascript">
function doOne()
{
    var r = Math.floor( 10 * Math.random() ); // choose one at random
    var h = document.test["unique" + r];
    h.value = (new Date()).getTime();
}
</script>
</head>
<body>
<form name="test">
    <input name="unique0" id="asdfadsf" /><br/>
    <input name="unique1" id="qwerqwer" /><br/>
    <input name="unique2" id="ertyerty" /><br/>
    <input name="unique3" id="aaaaaaaa" /><br/>
    <input name="unique4" id="bbbbbbbb" /><br/>
    <input name="unique5" id="cccccccc" /><br/>
    <input name="unique6" id="dddddddd" /><br/>
    <input name="unique7" id="eeeeeeee" /><br/>
    <input name="unique8" id="ffffffff" /><br/>
    <input name="unique9" id="zambonis" /><br/>
    <br/>
    <input type="button" value="demo" onclick="doOne()"/>
</form>
</body>
</html>
As you can see, it works. It just picks one of the 10 inputs randomly, but the concept is right.

Bring up your PHP page in your browser and then do a VIEW==>>SOURCE to see the HTML you are generating. If you can't see the bug by doing that, show us the HTML.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.