简体   繁体   中英

jquery.ajax with php

I have just started working on php. It's a very good lang as I'm feeling but some point I get stuck as I'm new to this.

My javascript code

var pv = $("#txtStart").val();
var av = $("#txtStartNextLevel").val();
var au = $("#fileStartPlay").val();
alert(pv+" "+av+" "+au);
var myau = au.split('\\');
$.ajax({
    type:"POST",
    url:php_url,
    data:"{startPoint:"+pv+"nextLevelPoint:"+av+"audioFile:"+myau[myau.length-1]+"}",
    contentType:"application/json",
    dataType:"json",
    success:function(){
        alert("done");
    },
    error:function(){
        alert(response);
    }
});

My PHP code.

<?php
    if(file_exists("Text.txt"))
    {
        $fileName = "Text.txt";
        $fh = fopen($fileName,"a")

        $Starts = $_POST["startPoint"];
        $NextLevel = $_POST["nextLevelPoint"];
        $AudioFileName = $_POST["audioFile"];
            $code .=$Starts."*".$NextLevel."_1*".$AudioFileName."\"";
            fwrite($fh,$code);
        fclose($fh);   
    }
?>

When I run this it executes but doesn't write the values in the variable

$Starts,$NextLevel,$AudioFileName**.

And further if I write the same ajax procedure in

$.post(php_url,{startPoint:pv,nextLevelPoint:av,audioFile:myau[myau.length-1]},function(data){});

this works fine and write the content in the file.

Also As I'm using post method it should not display the values in Address bar what I'm passing to write. But it's showing those values in both the method.

localhost://myphp.php?txtStart=Start&fileStartPlay=aceduos.jpg&txtStartNextLevel=adfd

Please guide me where I'm lacking...

Replace the value bellow (with quotas)

"{startPoint:"+pv+"nextLevelPoint:"+av+"audioFile:"+myau[myau.length-1]+"}"

to

{startPoint:pv, nextLevelPoint: av, audioFile: myau[myau.length-1]}

Do what Burak TAMTURK said, and also get rid of

contentType:"application/json",

$_POST data should be in content-type application/x-www-form-urlencoded , which is the default.

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