ruzD ruzD - 9 months ago 48
Javascript Question

jquery Post value to PHP webpage

I have made a web page with a dropdown menu. The idea is when we click any option of the dropdown menu it open produccion.php with a POST parameter 'edicion'. This parameter I use to do a query in a Database.

At moment the problem is that it's open the produccion.php but I think that doesn't POST the value of the variable

Jquery code:

$('#drEdicion a').click(function(event){

var ed = $(this).text();

data:{edicion: ed},
window.location.href = 'produccio.php'


Is it possible that the problem is when it's send via ajax?

Thanks in advance.

Answer Source

Your code is currently POSTing the content of the hyperlink over to produccio.php via AJAX. When that call completes it is then redirecting the user to the same page but without the POSTed value. Based on your comment:

I want to open produccion.php after selection option in dropdown button. With this option I do a query

it appears you actually want to open produccio.php with the POSTed value in the browser.

By far the easiest solution to the problem would be to alter the PHP code to accept the value of edicion from $_GET["edicion"] instead, and output your hyperlink as:

<a href="produccio.php?edicion=...">...</a>

This will also provide a better experience for users, especially if they have issues with JavaScript (or, for example, happen to be a search engine). However, if you're set on only using $_POST, the following code may - I've not tested this myself - allow this:

$('#drEdicion a').click(function(event){

    var ed = $(this).text();
    $('<form action="produccio.php" method="POST">' + 
          '<input type="hidden" name="edicion" value="' + ed + '" />' +

(with credit to the top answer on Dynamically create and submit form)