[javascript] 'xxx' is undefined error

Scorpio -  
kij_82 Posted messages 4102 Registration date   Status Contributor Last intervention   -
Hello,

I have a small function that allows me to add a new section of a form if one is modified.
I have copied a script from the internet (which I admit I do not master...) that is supposed to include the form code, like the include function in PHP.

However, it always returns the error 'undefined intervention'...

Here is my script

<script type="text/javascript"> function include(fileName) { if (document.getElementsByTagName) { Script = document.createElement("script"); Script.type = "text/javascript"; Script.src = fileName; Body = document.getElementsByTagName("BODY"); if (Body) { Body[0].appendChild(Script); } } } </script>


as well as the line that executes it

<input type="text" size="40" name="youhou" onchange="include(intervention.php)">

That's it... I am a beginner in JavaScript so the error could be anywhere ^^

Thanks to anyone who tries to help me.

7 answers

Scorpio
 
Sure, thanks, I understand my mistake...

But I don’t know how to fix it.
With what you provided, I know how to write and use what I’ve written.
But in my case, it’s about inserting all the code contained in the intervention.php page...

Thanks again.
1
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 938
 
The error is here:
<input type="text" size="40" name="youhou" onchange="include(intervention.php)"> 

It is quite fundamental by the way. It is important to distinguish between:

- HTML, CSS, and javascript code: understandable by the browser. JavaScript code is completely independent from the Apache server; it is only interpreted by the browser. In particular, you can run a web page that only has HTML/CSS/javascript without a web server.

- PHP code: it is interpreted by the web server (Apache server). The PHP code can only be understood by it. The Apache server converts the PHP code into HTML code that is understandable by the browser. In particular, when you make a request to a database, the Apache server must return the result of the request in the form of HTML code that is understandable by the browser.

So here you are triggering a JavaScript event to include a PHP file. However, including the PHP file only makes sense on the Apache server, therefore within a section of PHP code!

I assume you would like to modify a part of the HTML code of the page on this JavaScript event. This is entirely feasible, but in pure JavaScript. For that, you can use div tags:
<DIV id="plop"> <!-- The area in which I will write with my JavaScript--> </DIV>

Then, you can retrieve this area in JavaScript with the getElementById() function. You thus retrieve the div object in a JavaScript variable. All that's left is to modify its content like a regular string, by changing the innerHTML field. In particular, you can put HTML code in this field.

An example here:
developpeur.journaldunet.com

Good luck.
0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 938
 
The answer is in the link!
0
Scorpio
 
What I meant to say is that right now we are displaying a "hard-coded" text.

I want to display a whole portion of code... Do I need to type it out exactly as it is? Sorry if I'm off track...

To explain my problem, when you arrive on the page, you have a list of names displayed in fields (first name, last name, date, etc...). Depending on the case, there can be 1 name, several, or none at all.

So that's done through a SQL query and a PHP include.

ON TOP OF THAT, I would like to have an empty list displayed (the same one but not filled in). If you want to enter a new name, you do it, and then another empty field appears to re-enter a name, etc. This is where JavaScript comes in.

So I already have the code typed in a PHP file (I have to change the names of the fields each time... field-name.&i, and we increment i.) and I want to retrieve it so I don't have to type it again.

Right now, it's just displaying the file name...

Thank you again and sorry ^^
0
mamiemando Posted messages 33228 Registration date   Status Moderator Last intervention   7 938
 
Your JavaScript function can retrieve the values entered in the form, or pass them as parameters to your JavaScript function (the PHP code being "called first" before the JavaScript code, and these parameters will be hard-coded at the time the Apache server sends the page with just the HTML/CSS/JS).

And you don't have to apologize!
0
Scorpio
 
ok thanks, I'll try it like that...

Right now I've temporarily dropped the problem and switched to a PHP function (with an "add a name" button).
But it's not as good, so I think I'll go back to JS.

Thanks again
0
kij_82 Posted messages 4102 Registration date   Status Contributor Last intervention   857
 
You can also use AJAX with JavaScript's XMLHttpRequest objects if you know how.

--
~ iclic @ left, iclic, iclic @ right, iclic, iclic
and there are no bugs, sir! ~
0