Manish Kumar
Manish Kumar

Reputation: 343

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...

Upvotes: 0

Views: 123

Answers (2)

Barmar
Barmar

Reputation: 780724

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.

Upvotes: 0

Burak Tamt&#252;rk
Burak Tamt&#252;rk

Reputation: 1239

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]}

Upvotes: 3

Related Questions