【问题标题】:ajax form using information from current post - wordpressajax 表单使用来自当前 post-wordpress 的信息
【发布时间】:2013-07-14 23:09:28
【问题描述】:

我正在关注此tutorial,以使用前端 ajax 从表单提交电子邮件,并在电子邮件中包含当前帖子标题等信息。请注意,我不是 php 或 ajax 专家。任何建议或资源将不胜感激。

我尝试了以下方法 - 但我下面的隐藏输入字段值仅返回一个字符串,我认为这是因为它在页面加载后被包含。

jQuery

function submit_me(){
jQuery.post(the_ajax_script.ajaxurl, jQuery("#theForm").serialize()
,
function(response_from_the_action_function){
jQuery("#response_area").html(response_from_the_action_function);
}
);
}

PHP

// enqueue and localise scripts
function plugin_enqueue_scripts() {
 wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
 wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', 'plugin_enqueue_scripts');


 // THE AJAX ADD ACTIONS
 add_action( 'wp_ajax_the_ajax_hook', 'the_action_function' );
 add_action( 'wp_ajax_nopriv_the_ajax_hook', 'the_action_function' ); // need this to serve non logged in users
 // THE FUNCTION
 function the_action_function(){
 /* this area is very simple but being serverside it affords the possibility of retreiving data from the server and passing it back to the javascript function */
 $name = $_POST['name'];
 $title = $_POST['title'];

 echo"Title: " . $title;// this is passed back to the javascript function
 die();// wordpress may print out a spurious zero without this - can be particularly bad if using json
 }
 // ADD EG A FORM TO THE PAGE
 function hello_world_ajax_frontend(){
 $the_form = '
 <form id="theForm">
 <input id="name" name="name" value = "name" type="text" />
 <input name="action" type="hidden" value="the_ajax_hook" />&nbsp; <!-- this puts the action the_ajax_hook into the serialized form -->
 <input id="submit_button" value = "Click This" type="button" onClick="submit_me();" />
 <input name="title" type="hidden" value="<?php the_title(); ?>" />
 </form>
 <div id="response_area">
 This is where we\'ll get the response
 </div>';
 return $the_form;
 }
 add_shortcode("hw_ajax_frontend", "hello_world_ajax_frontend");
 ?>

【问题讨论】:

    标签: php ajax wordpress jquery


    【解决方案1】:

    试试这个:

    function hello_world_ajax_frontend(){
     $the_form = '
     <form id="theForm">
     <input id="name" name="name" value = "name" type="text" />
     <input name="action" type="hidden" value="the_ajax_hook" />&nbsp; <!-- this puts the action the_ajax_hook into the serialized form -->
     <input id="submit_button" value = "Click This" type="button" onClick="submit_me();" />
     <input name="title" type="hidden" value="'.get_the_title().'" />
     </form>
     <div id="response_area">
     This is where we\'ll get the response
     </div>';
     return $the_form;
     }
    

    基本上,只需将 get_the_title() 的结果与您的 html 输出连接起来。

    如果您将 php 代码块留在字符串中,它将不会被执行,并且您的 php 代码将被输出到 html 文档中。

    【讨论】:

    • 谢谢我试过了,但问题是使用 the_title() 没有返回值。使用 get_the_title();效果很好。
    【解决方案2】:

    更新:

    代替&lt;?php the_title(); ?&gt; 试试' . get_the_title() . '

    【讨论】:

    • 仍然产生一个字符串。
    • get_the_title(); 确实将标题作为字符串返回。字符串是空白的吗?您的预期结果是什么?
    • 很高兴您找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多