Jump to content


Photo

Javascript with PHP


  • Please log in to reply
16 replies to this topic

#1 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 04:43 PM

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>";

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

#2 MilesWilson

MilesWilson
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 July 2006 - 04:48 PM

Hi

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

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:

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

Cheers

M

#3 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 04:49 PM

No change :( I tried that at first, because it dint work, i changed it to \"checkbox\"

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 July 2006 - 04:56 PM

Look at your generated source to see if the Javascript is being generated correctly.

Ken

#5 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 04:59 PM

<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>


Thats my generated Code. it seems alright to me.

#6 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:02 PM

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

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>")
}


PHP 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>\")
}";



#7 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 14 July 2006 - 05:05 PM

if your document.write there is a

value="prot">

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

NoGray.com


#8 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:10 PM

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?
document.write(\"Residue Id : <input type='text' name='resid' onClick='add()'><br>\")


#9 Oldiesmann

Oldiesmann
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationCincinnati, Ohio

Posted 14 July 2006 - 05:14 PM

You want \"add()\" or 'add()', not /"add()/".
The Oldiesmann
SMF Project Manager
SMF+Gallery2 - Beta3.1 now available

#10 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:16 PM

\"add()\" doesnt work. Thats what i tried, that last one was a typo, sorry.

#11 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 14 July 2006 - 05:27 PM

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
<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>

NoGray.com


#12 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:41 PM

Doesnt seem to work again now. I think i screwed up something.

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\">";



#13 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:45 PM

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?

#14 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 14 July 2006 - 06:00 PM

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
<div id=\"code\"></div>

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

NoGray.com


#15 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 06:03 PM

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?

#16 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 14 July 2006 - 09:13 PM

resid[] will work and it'll pass an array to you php file.

NoGray.com


#17 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 14 July 2006 - 09:19 PM

yes, you'll need to give the inputs an array name if you want to process multiple inputs.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users