在一页上发送并打印 PHP 数组结果?

发布于 2024-12-04 16:02:00 字数 2361 浏览 0 评论 0原文

我是一名 PHP 初学者,今天我开始学习 Mootools。我正在尝试一个自动完成搜索框,它会在您提交表单后发送一个数组。您可以输入多个名称并搜索所有名称。

我想做的是在 mootools 中获取该数组,将其发送到 PHP 并在不离开页面的情况下显示结果。

这是原始文件

// Autocomplete initialization
var t4 = new TextboxList('form_tags_input_3', {unique: true, plugins: {autocomplete: {}}});

// sample data loading with json, but can be jsonp, local, etc. 
// the only requirement is that you call setValues with an array of this format:
// [
//  [id, bit_plaintext (on which search is performed), bit_html (optional, otherwise plain_text is used), autocomplete_html (html for the item displayed in the autocomplete suggestions dropdown)]
// ]
// read autocomplete.php for a JSON response example

t4.container.addClass('textboxlist-loading');               
new Request.JSON({url: 'autocomplete.php', onSuccess: function(r) {
  t4.plugins['autocomplete'].setValues(r);
  t4.container.removeClass('textboxlist-loading');
}}).send();     

这是 autocomplete.php

$response = array();
$names = array('Some name', 'Some name 2', 'etc');

// make sure they're sorted alphabetically, for binary search tests
sort($names);

foreach ($names as $i => $name)
{
    $filename = str_replace(' ', '', strtolower($name));
    $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
}

header('Content-type: application/json');
echo json_encode($response);

Submit.php

<?php
print_r($_POST);
?>

这就是我所做的

我的 php:

<?php
$result['msg'] = print_r($_POST);

echo json_encode($result);
?>

这是我在 index2.php 文件中所做的更改:

<input type="submit" name="submitform" value="Submit" id="submitform" />
<p id="response"></p>  

$('#submitform').addEvent('click', function(){

  new Request.JSON({  
        url: "inc/php/json.php",  
        onSuccess: function(response){  

        $('response').set('html',+data.result);  

        }  
    }).get($('findnames')); 


});

当您按提交,什么也没发生,所以显然我在某个地方犯了一个我似乎无法弄清楚的错误。

I am a PHP beginner and today I started learning Mootools. I am experimenting with a autocomplete search-box that sends out an array after you submit the form. You can type multiple names and search for all of them.

What I'm trying to do is to take that array in mootools, send it to PHP and display the results without leaving the page.

Here is the original file

// Autocomplete initialization
var t4 = new TextboxList('form_tags_input_3', {unique: true, plugins: {autocomplete: {}}});

// sample data loading with json, but can be jsonp, local, etc. 
// the only requirement is that you call setValues with an array of this format:
// [
//  [id, bit_plaintext (on which search is performed), bit_html (optional, otherwise plain_text is used), autocomplete_html (html for the item displayed in the autocomplete suggestions dropdown)]
// ]
// read autocomplete.php for a JSON response example

t4.container.addClass('textboxlist-loading');               
new Request.JSON({url: 'autocomplete.php', onSuccess: function(r) {
  t4.plugins['autocomplete'].setValues(r);
  t4.container.removeClass('textboxlist-loading');
}}).send();     

Here's autocomplete.php

$response = array();
$names = array('Some name', 'Some name 2', 'etc');

// make sure they're sorted alphabetically, for binary search tests
sort($names);

foreach ($names as $i => $name)
{
    $filename = str_replace(' ', '', strtolower($name));
    $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
}

header('Content-type: application/json');
echo json_encode($response);

Submit.php

<?php
print_r($_POST);
?>

And here is what I did

My php:

<?php
$result['msg'] = print_r($_POST);

echo json_encode($result);
?>

And here are the changes I made in the index2.php file:

<input type="submit" name="submitform" value="Submit" id="submitform" />
<p id="response"></p>  

$('#submitform').addEvent('click', function(){

  new Request.JSON({  
        url: "inc/php/json.php",  
        onSuccess: function(response){  

        $('response').set('html',+data.result);  

        }  
    }).get($('findnames')); 


});

When you press submit, nothing happens, so obviously I made an error somewhere that I can't seem to figure out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-12-11 16:02:00
$result['msg'] = print_r($_POST);

你不能这样做,print_r直接打印数组内容到页面,所以你不能将它存储在变量中。

也许您会使用这个:

<input type="button" onclick="SomeFunction(); name="submitform" value="Submit" id="submitform" />

只需将 SomeFunction(); 更改为其他内容即可。

编辑:
我检查了 Chrome 错误控制台,它的内容如下:

Uncaught TypeError: Cannot call method 'addEvent' of null

所以 #submitform 由于某种原因似乎无效。请尝试我的功能,使其正常工作。

$result['msg'] = print_r($_POST);

you cannot do this, print_r directly prints the array contents to page, so you cannot store it in variable.

Maybe you would use this:

<input type="button" onclick="SomeFunction(); name="submitform" value="Submit" id="submitform" />

Just change SomeFunction(); to something else.

EDIT:
I checked Chrome error console and here´s what it says:

Uncaught TypeError: Cannot call method 'addEvent' of null

So #submitform seems not to be valid in some reason. Please try my function thing to make it work like it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文