Jump to content

Archived

This topic is now archived and is closed to further replies.

vidyashankara

Javascript with PHP

Recommended Posts

[code]
echo "<head>

<script language=\"javascript\">";

echo "function add() {
var i = 1;
document.write(\"<input type=\"checkbox\" name=prot[] value=\"prot\"> Selection of Protonation of Residues:<br>SEGID : <select name=\"segid\">";

for ($i=0; $i<count($atm); $i++) {
echo"<option value=\"PRO$atm[$i]\">PRO$atm[$i]";
}

echo "</select>\")
document.write(\"Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>\")
document.write(\"Residue Id : <input type=\"text\" name=\"resid\" + i onClick=\"add()\"><br>\")
}";

echo "function cat(txt) {
alert(txt)
}";

echo "
</script>
</head>";


echo "<br>
<input type=checkbox name=prot[] value='prot'> Selection of Protonation of Residues:<br>";

echo "SEGID : <select name=segid> ";


for ($i=0; $i<count($atm); $i++) {
echo "<option value=PRO$atm[$i]>PRO$atm[$i]";
}

echo "</select>



Residue :
<select name=resi>
<option value=ASP>ASP
<option value=GLU>GLU
<option value=LYS>LYS
</select>
Residue Id : <input type=\"text\" name=\"resid\" onClick=\"cat('Hello')\"><p>";
[/code]

Hi, this is the code that i have right now. For some reason, i cant call the javascript function when i click on the text box. All the variable like $atm is defined earlier in the script. I added another function "cat" just to test if its working, but its not calling javascript at all. how do i do it?

Thanks
Vidyashankara

Share this post


Link to post
Share on other sites
Hi

Perhaps I'm wrong, but doesn't
[code]
document.write(\"<input type=\"checkbox\"
[/code]

Essentially open the double quotes, and then close them for 'checkbox' and then open them again?

If that is the case, that is where you're problem is. Try using a single quote:

[code]document.write(\"<input type='checkbox'[/code]
and see what happens.

Cheers

M

Share this post


Link to post
Share on other sites
Look at your generated source to see if the Javascript is being generated correctly.

Ken

Share this post


Link to post
Share on other sites
[code]
<script language="javascript">
function add() {
var i = 1;
document.write("<input type='checkbox' name=prot[] value="prot"> Selection of Protonation of Residues:<br>SEGID : <select name="segid"><option value="PROA">PROA<option value="PROB">PROB<option value="PROC">PROC<option value="PROD">PROD</select>")
document.write("Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>")
document.write("Residue Id : <input type="text" name="resid" + i onClick="add()"><br>")
}
function cat(txt) {
alert(txt)
}
</script>
</head><br>
<input type=checkbox name=prot[] value='prot'> Selection of Protonation of Residues:<br>SEGID : <select name=segid> <option value=PROA>PROA<option value=PROB>PROB<option value=PROC>PROC<option value=PROD>PROD</select>


Residue :
<select name=resi>
<option value=ASP>ASP
<option value=GLU>GLU
<option value=LYS>LYS
</select>
Residue Id : <input type="text" name="resid" onClick="cat('Hello')"><p><br>

[/code]

Thats my generated Code. it seems alright to me.

Share this post


Link to post
Share on other sites
Ok, I removed the Function add() and the function cat() started to work alright, so There's something wrong with the first function.

The Generated Code
[code]

function add() {
var i = 1;
document.write("<input type='checkbox' name=prot[] value="prot"> Selection of Protonation of Residues:<br>SEGID : <select name="segid"><option value="PROA">PROA<option value="PROB">PROB<option value="PROC">PROC<option value="PROD">PROD</select>")
document.write("Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>")
document.write("Residue Id : <input type="text" name="resid" + i onClick="add()"><br>")
}

[/code]

PHP Code

[code]
function add() {
var i = 1;
document.write(\"<input type='checkbox' name=prot[] value=\"prot\"> Selection of Protonation of Residues:<br>SEGID : <select name=\"segid\">";

for ($i=0; $i<count($atm); $i++) {
echo" <option value=\"PRO$atm[$i]\">PRO$atm[$i]";
}

echo "</select>\")
document.write(\"Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>\")
document.write(\"Residue Id : <input type=\"text\" name=\"resid\" + i onClick=\"add()\"><br>\")
}";

[/code]

Share this post


Link to post
Share on other sites
if your document.write there is a

value="prot">

change all the " to ' inside the document.write and it should work.

Share this post


Link to post
Share on other sites
Dude, you are amazing dude. Its working now!

thanks a lot :)

Just one little problem

in the following line, the onClick doesnt work, i tried /"add()/" and it still doesnt work. what do i do?
[code]
document.write(\"Residue Id : <input type='text' name='resid' onClick='add()'><br>\")
[/code]

Share this post


Link to post
Share on other sites
the onclick='add()' doesn't work because you are using document.write()

document.write will over write the whole document (including your javascript code), you will need to use innerHTML to add the HTML to a div or another element

Here is a sample HTML and JavaScript code
[code]
<html>
<head>
<title></title>
<script language="javascript">
function add() {
var i = 1;
var txt = "<input type='checkbox' name=prot[] value='prot'> Selection of Protonation of Residues:<br>SEGID : <select name='segid'><option value='PROA'>PROA<option value='PROB'>PROB<option value='PROC'>PROC<option value='PROD'>PROD</select>";
txt += "Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>";
txt += "Residue Id : <input type='text' name='resid' onClick='add();'><br>";

document.getElementById('code').innerHTML += txt;

}
</script>
</head>
<body onload="add();">
<div id="code">

</div>
</body>
</html>
[/code]

Share this post


Link to post
Share on other sites
Doesnt seem to work again now. I think i screwed up something.

[code]
echo "<head>

<script language=\"javascript\">";

echo "
function add() {
var txt = \"<input type='checkbox' name=prot[] value='prot'> Selection of Protonation of Residues:<br>SEGID : <select name='segid'>";
for ($i=0; $i<count($atm); $i++) {
echo "txt += <option value='PRO$atm[$i]'>PRO$atm[$i]";
}
echo "txt +=</select>\")
txt +=\"Residue : <select name=resi> <option value=ASP>ASP <option value=GLU>GLU <option value=LYS>LYS </select>\";
txt +=\"Residue Id : <input type='text' name='resid' onClick='add()'><br>\");
document.getElementById('code').innerHTML += txt;

}";


echo "
</script>
</head>";


echo "<br>
<input type=checkbox name=prot[] value='prot'> Selection of Protonation of Residues:<br>";

echo "SEGID : <select name=segid> ";


for ($i=0; $i<count($atm); $i++) {
echo "<option value=PRO$atm[$i]>PRO$atm[$i]";
}

echo "</select>



Residue :
<select name=resi>
<option value=ASP>ASP
<option value=GLU>GLU
<option value=LYS>LYS
</select>
Residue Id : <input type=\"text\" name=\"resid\" onClick=\"add()\"><p>
<body onload=\"add();\">
<div id=\"code\">";
[/code]

Share this post


Link to post
Share on other sites
I got it! Just some stupid mistakes.

Now, when i click the text box, The 3 boxes show up right at the bottom of the document, can i make it show up right after the text box?

Share this post


Link to post
Share on other sites
this is just HTML code, you need to make sure it's clean

for example, the <body onload=\"add();\"> shouldn't be in the middle of the page, you can just take it out (I added it just in the example)


the <div id=\"code\"> should have a close tag
[code]
<div id=\"code\"></div>
[/code]

You can put it anywhere you want (for example, before the <p>)

Share this post


Link to post
Share on other sites
nogray.. you rock! Thanks a lot!

and just one more question, now we have so many text boxes, how do i read the data submitted in this box? The script reads only the value in the last box. should i put the name of the form as resid[] ? instead of just resid?

Share this post


Link to post
Share on other sites
resid[] will work and it'll pass an array to you php file.

Share this post


Link to post
Share on other sites
yes, you'll need to give the inputs an array name if you want to process multiple inputs.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.