Chris K.
Chris K.

Reputation: 11

Use variable from post method in php file

I currently have 2 files:

the movie.php will have a javascript function deletemovie(movietitle).

The function is triggered when an item is clicked.

<button class='btn btn-danger' onclick = deletemovie('{$movie['title']}')>X</button>

and the deletemovie(movietitle) is :

function deletemovie(movietitle){
    console.log(movietitle);
    $.post("deletemovie.php", movietitle, "json");
  }

My question is, when I am handling the post data in deletemovie.php,

$title = $_POST[movietitle];
foreach ($movies as $movie){
    if ($movie['title'] == movietitle) {
        unset($movie['title']);
    } 
}

should I write like this? I think $title = $_POST[movietitle]; is somehow wrong.

Any suggestion?


my current code is :

movie.php

function deletemovie(movietitle){
  console.log(movietitle);
  $.post("deletemovie.php", movietitle, "json");
}

deletemovie.php

foreach ($movies as $movie){
if ($movie['title'] == $_POST['movietitle']) {
    unset($movie['title']);
}
}

i think the unset() isn't wrong? But the problem still not yet solved.


the movie.json:

{
    "abc": {
        "title": "abc",
        "director": "ddd",
        "rating": "5",
        "subtitle": "Yes",
        "genre": "I",
        "category": "I",
        "release": "2018-05-03",
        "end": "2018-05-09",
        "link": "das",
        "synopsis": "dasdas"
    },
    "afs": {
        "title": "afs",
        "director": "fasf",
        "rating": "5",
        "subtitle": "Yes",
        "genre": "I",
        "category": "I",
        "release": "2018-05-09",
        "end": "2018-05-10",
        "link": "fsa",
        "synopsis": "fs"
    }
}

Upvotes: 1

Views: 34

Answers (3)

Sohrab Yousefi
Sohrab Yousefi

Reputation: 116

Convert your json to php array like this:

$php_array = json_decode($json_value, true);

$php_array content:

[
   "abc" => [
        "title"=>"abc",
        "director"=>"ddd",
        "rating"=>"5",
        "subtitle"=>"Yes",
        "genre"=>"I",
        "category"=>"I",
        "release"=>"2018-05-03",
        "end"=>"2018-05-09",
        "link"=>"das",
        "synopsis"=>"dasdas"
    ],
    "afs" => [
        "title"=>"afs",
        "director"=>"fasf",
        "rating"=>"5",
        "subtitle"=>"Yes",
        "genre"=>"I",
        "category"=>"I",
        "release"=>"2018-05-09",
        "end"=>"2018-05-10",
        "link"=>"fsa",
        "synopsis"=>"fs"
    ]
]

Then unset the movie which is posted:

foreach($php_array as $movie=>$details){
   if($details['title'] == $_POST['movietitle']){
      unset($php_array[$movie]);
   }
}

And convert the result to json:

$json_output = json_encode($php_array);

Now, content of $json_output is the list of movies without $_POST['movietitle'] in json format.

Upvotes: 0

lopamudra
lopamudra

Reputation: 49

You should write the movietitle within apostophes

$title = $_POST['movietitle'];    

Upvotes: 0

Matthias B&#246;
Matthias B&#246;

Reputation: 459

Did you try putting "movietitle" in apostrophes?

$title = $_POST['movietitle'];

Otherwise php will probably interpret movietitle as an unknown constant and not find anything.

Also, this line looks rather wrong:

if ($movie['title'] == movietitle)

You probably want:

if ($movie['title'] == $_POST['movietitle'])

Upvotes: 2

Related Questions