0

This is a continuation of my question here.

Pass Text Input Variable to PHP for Div Loading

The solution to that question looks like this:

HTML:

<script type="text/javascript">
    function goSearch(){
        $("#Moviesearchdiv").load("MovieSearch.php",$('#mySearch').serialize());
     }
</script>

<form action="JavaScript:goSearch()" method="POST" id="mySearch" >
    <input type="text" name="moviename">
    <input type="submit" name="submit" value="Search" >
</form>

<div id="Moviesearchdiv"></div>

My MovieSearch.php looks something like this:

<?php
$themovie = $_POST["moviename"];
$myurl = "http://my-url-here.com/search/?q={$themovie}";

    $response = file_get_contents($myurl);
    $result = json_decode($response, true);
    $result=$result['movies'];

    $totality = count($result);

    for ($i = 0; $i <$totality; $i++) {
        $mymovietitle = $result[$i]['original_title'];
        $myposter = $result[$i]['poster_original'];
        $uniquemovieID = $result[$i]['movie_id'];
        echo "<li><img src={$myposter} >{$mymovietitle}</li>";
    }
?>

At the bottom of the php, within each found result, I want to have a button where I can add that movie to my collection. This is done with another API call that basically appends the 'movie-id' code to the end of the API url. So the bootom of the php above would look something like:

    for ($i = 0; $i <$totality; $i++) {
        $mymovietitle = $result[$i]['original_title'];
        $myposter = $result[$i]['poster_original'];
        $uniquemovieID = $result[$i]['movie_id'];
        echo "<li><img src={$myposter} >{$mymovietitle}

        <form action='JavaScript:addMovie()' method='POST' id='{$uniquemovieID}' >
            <input type=hidden id=movieid  name=movieid value={$uniquemovieID }>
            <input type='submit' name='submit' value='Add'  style='width: 50px;'>
        </form>

        </li>";
    }

    <SCRIPT LANGUAGE="JavaScript">
    function addMovie()
    {
        $("#Moviesearchdiv").load("movieadd.php",$("#yourFormId2").serialize());  
    }
    </SCRIPT>

And finally the movieadd.php file looks like:

<?php
$themovieID = $_GET["movieid"];

$addurl = "http://my-api-url/movie.add/?identifier={$themovieID }";

$response = file_get_contents($addurl);
$result = json_decode($response, true);
$result=$result['success'];
echo $result;

if ($result$ == 'True') {
    echo "Movie was SUCCESSFULLY Added!";

} else {
    echo "Sorry, something went wrong...";
}


?>

Because the search return various movies, the form "id" for each movie needs to be unique. I would like to pass the PHP variable $uniquemovie to the JS function line replacing the #yourFormId2 value.

 $("#Moviesearchdiv").load("movieadd.php",$("#yourFormId2").serialize());

I hope someone can help... Any help advice is greatly appreciated. Thanks for taking the time to read this.

Hernando

1
  • When you load MovieSearch.php you are sending moviename via GET there not POST. Commented May 3, 2014 at 13:53

1 Answer 1

1

You can pass the id itself to javascript.

<form action='JavaScript:addMovie('{$uniquemovieID}')' method='POST' id='{$uniquemovieID}' >
    <input type=hidden id=movieid  name=movieid value={$uniquemovieID }>
    <input type='submit' name='submit' value='Add'  style='width: 50px;'>
</form>


<SCRIPT LANGUAGE="JavaScript">
    function addMovie( movieId )
    {
        $("#Moviesearchdiv").load("movieadd.php",$("#" + movieId).serialize());  
    }
</SCRIPT>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you KNaito... I tried your code and did not have any luck, when inspecting the output, the form code was formatted as <form action="JavaScript:addMovie(" tt1630029')'="" method="POST" id="tt1630029"> the tt1630029 is the correct movieID.
GOT IT TO WORK!!!! The correct syntax above is <form action='JavaScript:addMovie(\"{$uniquemovieID}\")' method='POST' id='{$uniquemovieID}' > Thank you very much KNaito, you did it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.