Javascript representing a form within a method

I have a method that outputs the order of a set of images. I need to post this to a new php page.

I have a form that is currently printing an order to the same page.

<form action="mainpage.php" method="post">
<div style="clear:both;padding-bottom:10px">

    <input type="Button" style="width:100px" value="Show order" onclick="saveImageOrder()">

</div>

      

Saveimageorder()

shows the image and stores the order in a variable called orderString

function saveImageOrder()
{
    var orderString = "";
    var objects = document.getElementsByTagName('DIV');

    for(var no=0;no<objects.length;no++)
    {
        if(objects[no].className=='imageBox' || objects[no].className=='imageBoxHighlighted')
        {
             if(orderString.length>0) orderString = orderString + ',';
             orderString = orderString + objects[no].id;
        }
    }
    document.getElementById('debug').innerHTML = 'This is the new order of the images(IDs) : <br>' + orderString;    
}

      

How to do it?

+1


source to share


2 answers


You can submit the form (note this is untested):

document.formname.submit();

      

If you need to change the action (submit page) first:



document.formname.action = 'some_other_url';

      

If you need to submit the form asynchronously, you need to use XMLHttpRequest or something similar.

+2


source


with a simple POST (no ajax) you need to save the result of your process (image order id lookup) in a form field:

<input type="hidden" name="imagesorder" value=""/>

      

in your function, you can set the value to this field after filling the orderString:

document.getElementsByName('imagesorder')[0].value = orderString;

      

then submit the form, you can do this by replacing



<input type="button" .../>

      

from

<input type="submit" .../>

      

on the server side you will get the value in the post collection (I'm not php dev)

$_POST['imagesorder']

      

+1


source







All Articles