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