0
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form>
<label>Seleziona l'ente</label>
<select name="data" id="data">
    <option value="PRO - 011142764">COMUNE DI AGLIE'</option>
    <option value="PRO - 011120674">COMUNE DI AGRATE CONTURBIA</option>
</select>
<input type="text" name="textfield" id="textfield" />
</form>

<script>
$('#data').change(function() {
    $.post("richiesta.php", { value: this.value });
    $('#textfield').val(this.value);
});
</script>
</body>
</html>

Here's my simple web page with 2 options. The input tag is just for testing the POST data.

Here's richiesta.php, that receives the POST data on change event (I followed this thread):

<?php
list($comparto, $ente) = explode("-", $_POST['data'], 2);
echo "comparto: $comparto, ente: $ente";
?>

Here's what happens when I trigger the change event (from Firebug). POST is ok: enter image description here

Unfortunately the response has empty variables ($comparto and $ente): enter image description here

What's the problem with the explode/list function?

Community
  • 1
  • 1
MultiformeIngegno
  • 6,959
  • 15
  • 60
  • 119
  • 1
    wrong line `list($comparto, $ente) = explode("-", $_POST['data'], 2);` it should be `list($comparto, $ente) = explode("-", $_POST['value'], 2);` – Mudaser Ali Dec 30 '14 at 02:34
  • @MudaserAli No. $_POST['data'] is correct. $_POST['data'] will contain the value of the option that was selected. – Mooseknuckles Dec 30 '14 at 02:54
  • 1
    @Mooseknuckles bro `$.post("richiesta.php", { value: this.value });` you are posting data to server with and on server you can access the posted value with `value` if you want to access your posted data on server side please update ajax post call to `$.post("richiesta.php", { data: this.value });` through it you will get posted data in `$_POST['data']` – Mudaser Ali Dec 30 '14 at 07:34

2 Answers2

0

I'm not overly familiar with javascript, so I tend to lean towards my own ways of solving this. That said, I think this would accomplish what you are trying to do (without the need for your script near the bottom of your code):

<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form action="richiesta.php" method = "post">
<label>Seleziona l'ente</label>
<select name="data" id="data" onchange = "this.form.submit()" >
    <option value="PRO - 011142764">COMUNE DI AGLIE'</option>
    <option value="PRO - 011120674">COMUNE DI AGRATE CONTURBIA</option>
</select>
<input type="text" name="textfield" id="textfield" onchange="this.form.submit()" />
</form>
</body>
</html>

Note that this will cause the form to submit if either the select is changed or if text is entered. If you want to give someone the opportunity to alter both of them and THEN submit, you'll need to change this up a bit (probably best to just add a submit button).

Then, richiesta.php could simply get the data from $_POST['data'] and explode it from there.

Mooseknuckles
  • 497
  • 3
  • 7
  • The problem isn't with the javascript code. In fact the input tag is there just to test the POST data sent with javascript and it's correctly populated. The problem, I think, is in richiesta.php that either doesn't "receive" the POST data or doesn't output them correctly. – MultiformeIngegno Dec 30 '14 at 03:01
  • Okay, again I don't fully understand javascript. I deal almost exclusively with html and php. But if I was just looking at your HTML and ignoring the script, your form doesn't have an action. Could that be the problem, or does the javascript take care of that? I tried your explode statement on my own and it worked fine, so I think it's something to do with your script at the bottom of the page sending the data. – Mooseknuckles Dec 30 '14 at 03:04
  • But either way, simply adding onchange="this.form.submit()" to your select tag probably fixes your problems... – Mooseknuckles Dec 30 '14 at 03:08
0

$_POST['data'] doesn't exist you send 'value' param, so use $_POST['value']

Neji
  • 198
  • 1
  • 2
  • 7
  • That's incorrect. `$_POST['data']` will contain the value of the ` – Mooseknuckles Dec 30 '14 at 02:56
  • I can't test this right now, will let you know as soon as I get to my pc – MultiformeIngegno Dec 30 '14 at 03:01