PDA

View Full Version : Javascript + Forms + Dynamic HTML + Mozilla


bal00nb0y
05-21-2004, 12:02 PM
Hey all, just registered. How you doing.

I coded a page where you click and image which triggers some javascript to change the HTML in a <div> to an input form. This enables someone to change text dynamically. After they are done editing the text in the input box, they hit the same button which submits the form and saves the data and reloads the page they were on (with the new data).

This all works fine. In IE. In Mozilla however, it submits blank info. I am assuming this is because it's not picking up the text input as part of the form. Any ideas? Here is the basic layout:



<javascript>
variable = "<input type=text name="new_text"
insert_new_stuff(variable);
</javascript>


<html>
<form>
<div id=dynamic_text>
stuff
</div>
</form>
</html>



Any suggestions? Thanks

agent002
05-21-2004, 12:06 PM
Hi, and welcome to HTMLForums! :)
I assume this is a JavaScript question. I'll move the thread to Client Side Scripting... and back here again, if I'm wrong.

Is that all of your code?? There is no such tag as <javascript>, you need to do <script type="text/javascript">. And the JavaScript can't be located above the <html> tag.

Maybe you are just trying to shorten up the code to show, to keep the post clean - but that code doesn't really tell me anything. Can you please post the full code?

bal00nb0y
05-21-2004, 01:14 PM
Thanks for the prompt reply. Here's the edited code for clarity. I've included comments and generic naming of things so it's not quite so confusing.


<html>
<head>
<script language="JavaScript">
<!--
if (document.images)
{
// Pre-load the images
image1= new Image(190,35);
image1.src="images/save.gif";

image2 = new Image(190,35);
image2.src="images/image2.gif";

image3= new Image(190,35);
image3.src="images/image3.gif";
}
var name_replace;
var email_replace;

// Notice that these are form inputs that are dynamically inserted and changed
// when the user clicks the image for the first time. These should be submitted
// when the forms are submitted (works in IE, not Mozilla, not sure why)
first_replace="<input name=\"first_box\" type=\"text\" value=\"sample1\"><br>";
second_replace="<input name=\"second_box\" type=\"text\" value=\"sample2\"><br>";

function div_replace(which_div, which_text, which_pic)
{
// If the image has already been changed (i.e. whether we are editing the text in the
// dynamically generated input form text box)
if (document[which_pic].src == eval(which_pic + "on.src"))
{
// If we're editing and they click the picture to save the content
// then save whichever form respectively
if (which_pic == "first_pic")
{

document.first_form.submit();
}
if (which_pic == "second_pic")
{
document.second_form.submit();
}
exit;
}
// The code that actually replaces the text with our input box
// Note: this only gets called if this hasn't been already done
// If it's already done, the previous lines will be called and then
// exited
document.getElementById(which_div).innerHTML = which_text;
change_submit(which_pic);
}

function change_submit(imgName)
{
// Change the image after text has been changed
if (document.images)
{
imgOn=eval(imgName + "on.src");
document[imgName].src= imgOn;
}
}
//-->
</script>
<style>
<!-- Fixes image hand mouseover for Mozilla by using two entries -->
.cursor {cursor: pointer; cursor: hand;}
</style>
</head>
<body>

<form name="first_form" action="reload.php" method="POST">
<div id="first_div">
<!-- this will be changed to the input box when the first image is clicked -->
Some dynamically generated text from PHP
</div>

<input type=hidden name="type" value="first_form">
<input type=hidden name="current_page" value="<? echo $_SERVER['PHP_SELF']; ?>">

<a onMouseOver="this.className='cursor';" onclick="javascript:div_replace('first_div', first_replace,'first_pic');">
<img name="first_pic" src="images/image1.gif" border=0 alt="Click to Edit">
</a>
</form>

<form name="second_form" action="reload.php" method="POST">
<div id="second_div">
<!-- this will be changed to the input box when the first image is clicked -->
Some dynamically generated text from PHP
</div>

<input type=hidden name="type" value="second_form">
<input type=hidden name="current_page" value="<? echo $_SERVER['PHP_SELF']; ?>">

<a onMouseOver="this.className='cursor';" onclick="javascript:div_replace('second_div', second_replace,'second_pic');">
<img name="second_pic" src="images/image2.gif" border=0 alt="Click to Edit">
</a>
</form>

</body>
</html>


Note that it works in IE, but not Mozilla or Safari(on the Mac)

agent002
05-21-2004, 01:29 PM
Change
document[imgName].src= imgOn;
and everything similiar, i.e. references using document[] to use document.getElementById():
document.getElementById(imgName).src= imgOn;

bal00nb0y
05-21-2004, 03:18 PM
I'm not having trouble with the image replacement, that seems to be fine. For some reason in Mozilla, it doesn't seem to be picking up the:


first_replace="<input name=\"first_box\" type=\"text\" value=\"sample1\"><br>";
second_replace="<input name=\"second_box\" type=\"text\" value=\"sample2\"><br>";


as part of the form when it gets submitted (it is submitting it, but the values of "first_box" and "second_box" are blank.

agent002
05-21-2004, 04:33 PM
Try replacing exit with return false.

bal00nb0y
05-24-2004, 08:36 AM
That didn't seem to do the trick. I did some debugging and replaced the form submit() with some alert() calls. The results were that the dynamic input boxes being inserted were not (as I suspected) being registered as part of the form (or at least part of the document.

alert(document.first_form.type.value);
would appropriately popup something that said "first_form" (from the hidden value type)

but

alert(document.first_form.first_box.value);

would not return anything. So the dynamically inserted box is not working for Mozilla. Arg..

agent002
05-24-2004, 09:04 AM
To me, it all works well in Firefox 0.8, I tried inserting the alert() and I'm alerted "sample1" (which is the value of first_box) both in IE and Firefox.