将文本输入变量传递给PHP以加载Div [英] Pass Text Input Variable to PHP for Div Loading

查看:82
本文介绍了将文本输入变量传递给PHP以加载Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,我想要一个输入框和一个搜索按钮。它会搜索匹配输入框的电影。搜索和解析是通过php来完成的,但我希望在原始页面上显示结果,作为更新的div。我有当前的html代码:

 < script type =text / javascript> 
函数goSearch(){
$(#Moviesearchdiv)。load(MovieSearch.php);
}
< / script>

< form action =JavaScript:goSearch()method =POST>
< input type =textname =moviename>
< input type =submitname =submitvalue =Search>
< / form>

< div id =Moviesearchdiv>< / div>

我的MovieSearch.php看起来像这样:

 <?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'];
echo< li>< img src = {$ myposter}> {$ mymovietitle}< / li>;
}
?>

当我按原样运行时...没有任何反应。如果我手动更改MovieSearch.php中的第一个变量来读取如下内容:

  $ themovie =Rocky; 

这会正确加载我所有的Rocky电影,而不会刷新页面。



如何正确地将moviename输入传递给php?



任何帮助和建议都很重要

谢谢,

Hernando



 函数goSearch (){
$(#Moviesearchdiv)。load(MovieSearch.php,$('#yourFormId')。serialize());
}

请注意,您需要为表单添加ID,并将#yourFormId



在调试这些类型的AJAX调用时,首先您需要知道您尝试与之通信的页面位于您的AJAX调用认为它的位置是。你知道这是因为当你填写$ themovie时 - 你会得到结果,你也可以在浏览器中的开发者工具中查看。第二,你需要确保你发送了你认为自己的数据,你可以通过返回一个print_r()或var_dump()来检查它是否为$ _POST,或者$ _GET取决于你使用的是什么(或者认为你在这种情况下使用)。



这两个步骤可以帮助您解决很多问题。


I have a web page where I want to have an input box and a search button. It will search for movies matching input box. The search and parsing is done via php, but I want the results presented on the original page, as an updated div. I have the current html code:

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

<form action="JavaScript:goSearch()" method="POST">
    <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'];
        echo "<li><img src={$myposter} >{$mymovietitle}</li>";
    }
?>

When I run it as is... nothing happens. If I manually change the first variable in MovieSearch.php to read something like:

$themovie = "Rocky";

This properly loads all the Rocky movies in my div as expected, without the page refreshing.

How can I properly pass the "moviename" input to the php?

Any help and advice is greatly appreciated.

Thanks,

Hernando

解决方案

a) you're not passing your data to the page (the second argument in .load should be your form data), so the value of $themovie is null.

b) Even though you have method="POST" on your form, using .load assumes GET unless data provided is an object. One method of providing your form data and in the format of an object (to send as POST) would be to:

function goSearch(){
   $("#Moviesearchdiv").load("MovieSearch.php",$('#yourFormId').serialize());
}

note you will need to add an ID to your form, and replace #yourFormId with it.

When debugging these types of AJAX calls, first you need to know that the page you're trying to communicate with is in the place your AJAX call thinks it is. You know that it is because when you fill in $themovie - you get results, you could also check this in developer tools in the browser.

Secondly you need to make sure you're sending the data you think you are, you could check this by returning a print_r() or var_dump() of $_POST or $_GET depending on what your using (or think you're using like in this case).

Those 2 steps will lead to you solving a good percentage of issues you find.

这篇关于将文本输入变量传递给PHP以加载Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆