简体   繁体   中英

Why this ajax script doesn't send data

i'm trying to send a js variable to a php file, to create a query. Thi is my script, but it doesn't work, could you help me?

<select class="form-control" id="Input_Cli" name="Input_Cli" onchange="sendtophp()">
    <option disabled selected value> Seleziona il cliente</option>
    <?php
       $results = pg_query($conn, "SELECT piva, nome FROM cliente");
       while($row = pg_fetch_array($results)) { ?>
            <option value="<?php echo $row['piva']?>"><?php echo $row['nome']?></option>
    <?php
       }
    ?>
</select>
<script type="text/javascript">
function sendtophp()  { 
    var cli = document.getElementById("Input_Cli").value;
    document.getElementById("prova").innerHTML = cli;
    $.ajax({
        url: "C:\xampp\htdocs\Progetto\getlist.php"
        type: "GET"
        data: {Cli : cli}
        success: function( )
        {
            alert("success!");
        }
    });
} 
</script>

I just tested this, your script works quite fine. There must be in error in your PHP outputting the options or you are simply calling the wrong url, which we cant check.

The below snippet with your PHP (test.php in the same directory as the html file) on a valid server returns 1 if I change the selectbox.

Make sure you are actually opening the page on your webserver in your browser, not your local file.

For the purpose of this snippet I changed the url to something we can actually validate here.

 function sendtophp() { var cli = document.getElementById("Input_Cli").value; $.ajax({ url: "https://jsonplaceholder.typicode.com/posts/1", type: "GET", data: { Cli: cli }, success: function(data) { console.log(data); } }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="form-control" id="Input_Cli" name="Input_Cli" onchange="sendtophp()"> <option disabled selected value> Seleziona il cliente </option> <option>some option</option> </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM