开发者

prototype ajax updater div with different buttons

i'm learning it, but i cant find what's wrong in this! i want the div2 to get data from the form in div1, called formulario. i would like to know which item is selected and which button was clicked.

main html file:

<script src="utils/Scripts/prototype.js" type="text/javascript"></script>
<script type="text/javascript">

function sendf(formul, divi, php)
    {
     var params = Form.serialize($(formul));                    
     new Ajax.Updater(divi, php, {method: 'post', parameters: params, asynchronous:true});
    }
</script>

</head>

<body>
<div id="div1"> 
contenido div1
<form id="formulario" method="POST">
<select size="3" id="lista" onchange="sendf('formulario', 'div2', 'prodiv1.php');"> 
<option>elemento 1</option>
<option>elemento 2</option>
<option>elemento 3</option>
<开发者_高级运维;/select>
<input type="button" id="b1" value="bot1" onclick="sendf('formulario', 'div2', 'prodiv1.php');" />
<input type="button" id="b2" value="bot2" onclick="sendf('formulario', 'div2', 'prodiv1.php');" />
</form>


<div id="div2" style="background: blue;"> 
contenido div2
</div>
</div>
</body>
</html>

the php file, prodiv1.php:

<?
echo 'exec: prodiv1.php<br>';
print_r($_POST);
echo serialize($_POST);

if (isset($_POST))
    {
    foreach ($_POST as $key=>$value)
        {
        echo $key.'=>'.$value."<br>";
        }
    }


echo "select: ".$_POST['lista'];

if (isset($_POST['b1'])) {echo 'click: boton1';} else {echo 'click: boton2';}
?>

i've tried a lot of things, and seen that it could be done with event observers, httprequests and such, but what i need is quite easy, and probably there's an elegant way to solve it... i thank in advance any help! have a nice day. guillem


if you dont need to actually process the form contents in some way then you have no need to use Ajax to pass to a PHP script. Depending on what exactly you wanted to display in div 2 you could do something as simple as this:

function sendf()
{
 var listvar = $('lista').value;
 $('div2').update('select menu value was ' + listvar);
}

This is obviously missing quite a lot of detail and can be massively improved but it should highlight the fact that AJAX is not required.


Edit Looking at the rest of the code you have posted, is AJAX really required for this? surely you are just updating the existing page with data already present on the page, the server has no real part to play in this?

Sorry to dive into jQuery again, but this should allow you to get the values into "div2" without an ajax request.

    $(document).ready(function() {
        $("input").click(function(e) {
            $("#div2").html($(this).attr("id")+" clicked<br />");
            updateList();
        });
    });
    function updateList() {
       $("#div2").append($("#lista").val() + " selected");
    }

In plain English this code says "if an input element is clicked, update the value of div2 with the input variables id, and append the selected value from the list to the result". Hopefully that makes sense to you :)

If you need an easy, elegant way to solve this with AJAX, use the jQuery library's ajax and post methods. For more information take a look here, it will significantly cut down on the size and complexity of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜