/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP and Javascript problem

Hi guys,

I have a really weird problem. I’m creating some fields dynamically with Javascript using document.createElement.

Each control has it’s id and name set.

After this script is run, with PHP I try to populate the newly created fields. I’m creating the fields FROM php, I just do an echo of the javascript function that creates the elements for me. And like I said all this is done before populating the fields.

Problem is that when I try to reference these newly created items, javascript complains that they don’t exist.

Viewing the source code of the web page actually doesn’t show the dynamically created items.

Any one have any idea of why this is happening or of a solution to this?

Thanks!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLApr 13.2010 — Do you have any code you could provide? It might help us understand what exactly is going on and what you are trying to achieve.

Cheers
Copy linkTweet thisAlerts:
@mavalos88authorApr 13.2010 — Sure, it looks something like this (condensed version of what I do):

This function creates my html element. For purposes of this example, assume newRowValue =1 so the element will be called Quantity1
[CODE]
<head>

function addRow(tableId) {
var table = document.getElementById(tableId);
newRowValue= document.getElementById('activeProducts').value;
newRowValue=parseInt(newRowValue)+1;
document.getElementById('activeProducts').value=newRowValue.toString();
var row = table.insertRow(newRowValue);
var i=1;
var cell1 = row.insertCell(0);
var optn;
var element1 = document.createElement("select");
element1.name= "Quantity"+newRowValue;
element1.id= "Quantity"+newRowValue;
for (i=1;i<100; i++){
optn = document.createElement("option");
optn.text = i;
optn.value = i;
element1.options.add(optn);
}
cell1.appendChild(element1);
}
</head>
[/CODE]


With php I add a new row with my javascript function (assume the table products exists and has no rows):

[CODE]
<html>
<table id='products'>
</table>

<?php
echo"
<script>
addRow('products');
</script>
";
?>
...
[/CODE]


after I add the row I try to populate the newly created element:

[CODE]
....

<?php
echo"
<script>
document.getElementbyId('Quantity1').value= 'some value';
</script>
";
?>
</html>

[/CODE]


The above line throws the error: document.getElementById("Quantity1") is null, which obviously means that the element doesn't exist.

The element does indeed exist though, it shows perfectly on my web page and it even is submitted if I try to do so.

Source code of the page doesn't show the newly created row and element though.

I transcribed this from the top of my head so there might be syntax errors (not logical errors though) so if you notice any, that's not the problem, my code runs fine.

Tried to be as clear as possible, hope you guys can help me out.

Thanks!
Copy linkTweet thisAlerts:
@mavalos88authorApr 13.2010 — Found the error, nothing to do with this, an error elsewhere.

Thanks though!
×

Success!

Help @mavalos88 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...