views:

63

answers:

2

Hey guys,

Trying to push some coordinates, as well as some stuff specified in a form by the user, to an an array called "seatsArray". Here's my code:

<div>
<img onLoad="shiftzoom.add(this,{showcoords:true,relativecoords:true,zoom:100});" id="image" src="plan1.bmp" width="1024" height="768">
</div>

<script type="text/javascript">
var seatsArray = [];
</script>
<br><input id="backnave" type="radio" name="area" value="backnave" /> Back Nave<br>
<input id="frontnave" type="radio" name="area" value="frontnave" /> Front nave<br>
<input id="middlenave" type="radio" name="area" value="middlenave" /> Middle nave<br>
<input type="radio" id="standardseat" name="seat" /><label for="radio1">Standard</label><br>
<input type="radio" id="wheelchairseat" name="seat" checked="checked" /><label for="radio2">Wheelchair</label><br>
<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<form id="my_form" action="savetext.aspx" method="post" onsubmit="return prepare()">
  <input type="text" id="file_name" name="file_name" rows="1" cols="20" />
  <input type="hidden" name="seatsArray" />
  <input type="submit" value="Save" />
</form>

<script type="text/javascript">
function prepare();
{
  document.getElementById('seatsArray').value = seatsArray.join();
  return true;
}
</script>

<script type="text/javascript">
var coordinates = document.getElementById("image");
coordinates.onclick = function(e) {
e = e || window.event;

if (e && e.pageX && e.pageX) {
            e.posX = e.pageX;
            e.posY = e.pageY;

        } else if (e && e.clientX && e.clientY) {
            var scr     = {x:0,y:0},
                object  = e.srcElement || e.target;
            //legendary get scrolled
            for (;object.parentNode;object = object.parentNode) {
                scr['x'] += object.scrollLeft;
                scr['y'] += object.scrollTop;
            } 
            e.posX = e.clientX + scr.x;
            e.posY = e.clientY + scr.y;
        } 

var desc = "";
if(document.getElementByID("backnave").checked) {
  desc = "BN, "+desc;
} else if(document.getElementByID("middlenave").checked) {
  desc = "MN, "+desc;
} else if(document.getElementByID("frontnave").checked) {
  desc = "FN, "+desc;
}

if(document.getElementById('wheelchairseat').checked) {
  //Wheelchair seat is checked
  desc = "Wheelchair "+desc;
}

seatsArray.push(desc + e.posX, e.posY);

}
</script>

But simply nothing is getting pushed to the array. I can tell this as I am using the following ASP.NET to write the array to a text file:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e) 
    {
        string path = Server.MapPath(".")+"/"+Request.Form["file_name"] + ".txt";
        if (!File.Exists(path)) 
        {
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine(Request.Form["seatsArray"]);
                sw.WriteLine("");
            }   
        }

        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Response.Write(s);
            }
        }
    }
</script>

The name of the text file is correct according to what the user put in the form "file_name". As you can see I made seatsArray a hidden form object so thats what the ASP.NET is trying to call.

Is something in the javascript in the wrong order or something? Because I just can't get it to fill up that text file.

Thanks!

A: 

Try dropping a few alert(seatsArray) statements in the javascript before and after you think it should change values and one right before the form is submitted. If you see the correct value then you know it's the back-end that is not right.

Upon further inspection: the prepare() function is looking for an element with id="seatsArray", which doesn't exist. You have two elements with name="seatsArray" but no id="seatsArray". The array is more than likely being populated but it is not being written back to the form.

Dan Iveson
alert comes up before I get the chance to click on my "image" element and push something to the array? thanks
IceDragon
A: 

In the browser, check for javascript errors. Firefox/IE reports 2. you have a semicolon that shouldn't be there. = > function prepare();

document.getElementById("image"); returns null, so coordinates.onclick fails

I recommend debugging in Firefox with firebug, or use developer tools with ie8 (press F12)

Edit: make sure to declare seatsArray as an array before use.

....

< script type="text/javascript" >

seatsArray = []; //insert this line

function prepare() {

....

David
that was not the full code, the "image" is elsewhere, so it would return that error. thanks, i removed the semicolon
IceDragon