PDA

View Full Version : .createelement help


aaronl
07-08-2008, 04:30 AM
been workong this for a few days trying to figure out this whole DOM thing.. so far.. no cake. its supposed to create a new textbox and then a <br> afterwards, but it just will not do it... thanks in advance.


<html>
<head>
<title></title>
</head>

<script type="text/javascript">
function addElement() {
var breaker = document.createElement("br");
var newDiv = document.createElement("input");
newDiv.setAttribute('type', 'text');
newDiv.setAttribute('id', 'food');

var my_div = document.getElementById("org_div1");

document.body.insertBefore(breaker, my_div);
document.body.insertBefore(newDiv, my_div);
}

</script>

<body>

<form>
<input type="text" id="food"></input>
<div id='org_div1'></div>
</form>

<a href="javascript:addElement()">click</a>

</body>
</html>

rangana
07-08-2008, 05:55 AM
See if adding the highlighted helps:

var my_div = document.getElementById("org_div1").parentNode;

aaronl
07-08-2008, 07:10 AM
thank you sir!

aaronl
07-08-2008, 07:40 AM
hmm well I hit another snag, as soon as I did an include for the file it stopped working again...

rangana
07-08-2008, 07:51 AM
Show us the code.

aaronl
07-08-2008, 08:08 AM
add_test.php (the part im having trouble with, the included file)




<form action="index.php?p=add_recipe_script" method="post">
<input type="text" id="food" name="ingredients" size="44">
<br />
<div id='org_div1'></div>
<input type="hidden" name="assume" value="true">
<input type="submit" value="submit">
</form>

<a href="javascript:addElement()">click</a>



the index.php page (the javascript from above is in the misc_scripts.js file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/css.css" media="screen" />
<script type="text/javascript" src="scripts/misc_scripts.js"></script>

<title>recipeboxv2.0</title>

</head>

<body>
<div id="topper">
<div id="nav_area">
<?php include "navigation.php"; ?>
</div>
</div>
<div id="wrapper">
<div id="header">
<div class="search"><a href="index.php?p=search_box">search</a></div>
<div class="recipebox">recipebox</div><div class="version">2.0</div>
</div>


<?php include "nav_script.php"; ?>

</div>


</body>
</html>


the nav_script.php page


<?php
$default = "default.php";
$error = "error.php";
$allowed = array (
'default',
'add_test'
);
if( isset( $_POST["p"] ) || isset( $_GET["p"] ))
{
$page = isset($_GET["p"]) ? $_GET["p"] : $_POST["p"];

if( in_array( trim ( $page ), $allowed ))
{
$file = $page . ".php";
if( (file_exists( $file )))
{
include( $file );
}
else
{
include( $error );
}
}
else
{
include( $error );
}
}
else
{
include( $default );
}
?>


let me know if you need anything else.

rangana
07-08-2008, 09:00 AM
I'm sorry, but I can't reproduce the problem at my end.

The part you're having problem with (add_test.php) is'nt called in any of the code you've shown. Neither on index.php nor on nav_script.php. Which page is add_test.php shown?

aaronl
07-08-2008, 09:22 AM
if i go to ?p=add_test it shows it. I posted up the nav_script and the index incase that made a difference. (nav_script is the redir and index is the rest of the page, where the test is the content)

rangana
07-08-2008, 09:56 AM
Yes. I understand now. My apologies, but PHP is'nt my forte. I just find it weird since the code works fine when it's in one file, but throws an error when it's inside the PHP include.

My apologies, but I'm on my wits end :(

Let's wait for other members to help you with this regards.

aaronl
07-08-2008, 09:58 AM
hey atleast you tried right :) thanks anyways!

rangana
07-08-2008, 10:30 AM
What happens when you move the highlighted on index.php instead:

<form action="index.php?p=add_recipe_script" method="post">
<input type="text" id="food" name="ingredients" size="44">
<br />
<div id='org_div1'></div>
<input type="hidden" name="assume" value="true">
<input type="submit" value="submit">
</form>

<a href="javascript:addElement()">click</a>

aaronl
07-08-2008, 07:04 PM
well i assumed you meant, move the highlighted to the index.php file directly, which I did and that worked... but Kinda defeated the purpose?? or is there something deeper going on?

edit: well I also found out that if I put the


<div id='org_div1'></div>


part outside of the form, it works as well (like inside add_test.php still but just not in the form)... but it puts it below the submit button and all of that stuff... which I need above them... any ideas?