views:

252

answers:

3

hello! 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

A: 

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.

Jay
no need to switch to jQuery for this, Prototype is more than adequate
seengee
Yeah jQuery definitely isn't needed for this, my original post was about AJAX which are much simpler to perform and describe in jQuery, as is the above operation (write less, do more).
Jay
+1  A: 

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.

seengee
@jay: he's using prototype so my code is correct, your code is jquery
seengee
It could be worth mentioning your javascript library in your answer, as it isn't initially clear that you're using prototype, and this code will not function without it :)
Jay
yeah but he has tagged his question with prototype and his code sample links to prototype, i would say thats pretty clear.
seengee
Meh, I'm just a blind idiot, didn't even see that in the subject/code. +1 mate, good job
Jay
haha no worries, cheers
seengee
A: 

well, in fact, this was a simplification, as the php is intended to do some calculations and send the answer to the div, so, in my case, prototype is needed.

the problem is that javascript scripts dont work. they seem not to be read, simply!

i've read that evalscripts: true in the declaration of updater (sendf in this case) should work, but doesn't.

thanks in advance for any help!!! guillem

guillem