将参数从Ajax传递到PHP文件 - php

我从AJAX调用了一个PHP文件,我还想传递一个变量。这是我的代码:

echo "<h4 style='color:white'>Messages</h4>";
echo "<textarea cols='50' rows='10' style='font-size: 24px;'></textarea><br><br>";
echo "<button id='sendmessage' style='padding:10px'>Submit</button>";
echo "<button id='deletemessage' style='margin-left:5px;padding:10px'>Delete</button>";


echo "<script>
    jQuery('#sendmessage').click(function(){
        jQuery.ajax({
            data: { content: jQuery('textarea').val() },
            url:'wp-content/themes/dt-chocolate/postmessages.php',
            success:function(data){}
        });
    });
    </script>"

和PHP文件:

<?php
require_once('/opt/lampp/htdocs/mydomain/wp-config.php');

$post = array(
        'post_content'   =>  data.content,
        'post_title'     =>  "testing",
        'post_status'    =>  'publish',
        'post_type'      =>  'post',
        'post_category'  =>  array(28)  // Default empty.
    );  



wp_insert_post( $post );
?>

但是,帖子的内容是“ datacontent”,而不是来自文本区域的实际文本。我究竟做错了什么?

参考方案

echo "<script>
jQuery('#sendmessage').click(function(){
    jQuery.ajax({
        data: { content: jQuery('textarea').val() },
        url:'wp-content/themes/dt-chocolate/postmessages.php',
        type: 'POST',
        success:function(data){}
    });
});
</script>";

--

$post = array(
    'post_content'   =>  $_POST['content'],
    'post_title'     =>  "testing",
    'post_status'    =>  'publish',
    'post_type'      =>  'post',
    'post_category'  =>  array(28)  // Default empty.
);  

.ajax type(方法)设置为'POST',则可以从$_POST数组读取PHP中的POST请求变量。

验证IBAN PHP - php

在设计新平台时,我们尝试集成IBAN编号。我们必须确保IBAN已经过验证,并且存储在数据库中的IBAN始终正确。那么验证数字的正确方法是什么? 参考方案 正如我在其他问题中解释的逻辑一样,我尝试自己创建一个函数。根据Wikipedia文章中解释的逻辑,在下面找到合适的功能。国家特定验证。它适合吗http://en.wikipedia.org/wiki/Int…

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…