【问题标题】:Store form data in MySQL with jQuery.ajax [duplicate]使用 jQuery.ajax 在 MySQL 中存储表单数据 [重复]
【发布时间】:2010-10-25 12:31:23
【问题描述】:

编辑:我找到了获取正确 URL 的解决方案。 Se the solution in this thread.

您好,我在处理表单时遇到问题,以便将其数据保存在我的 MySQL 数据库中。我使用 Wordpress 作为 CMS。

我用过这个例子:http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form

我很确定我的问题的根源是我在 javascript 中使用了错误的 url。错误消息只返回“未定义”,Firebug 报告页面未找到 404 错误。

那么正确的网址是什么? 任何帮助将不胜感激。

这是我的网站结构:

Mywebsite (folder)      
  sl_register.tpl.php   
  includes    (folder)    
  storelocator (folder)
    process_frm_store.php
    frm_store.php
  js (folder)
    myscripts.js

这就是我建立网站的逻辑:

sl_register.tpl.php:

<?php
/*
  Template Name: SL - Register Store
*/  
  get_header();
  include_once 'includes/storeLocator/frm_store.php';
  get_footer();
?>

frm_store.php:

<form id="store_data_form" class="appnitro"  method="post" action="">
  <input id="store_active" name="store_active" type="hidden" value="pending" />
  <input id="store_name" name="store_name" type="text" value=""/> 
  <input id="store_street1" name="store_street1" type="text" value="" />
  <input id="saveForm" class="submitButton" type="submit" name="save" value="Save" />
</form>

process_frm_store.php:

<?php
  $myDB = new DAL(); 
  $myDB->connect();

  if (isset($_POST['save'])) 
  {
    $formData =  array(
      "name"=> mysql_real_escape_string($_POST['store_name']),
      "street1"=> mysql_real_escape_string($_POST['store_street1']),
      "zipcode"=> mysql_real_escape_string($_POST['store_zipcode']));

    $myDB->addNewStore($formData);
  }
?>

myscripts.js:

jQuery.processForms = function()
{
  jQuery('form#store_data_form').submit(function() 
  {
    var store_name = 'Test store'; //jQuery("input#store_name").val();
    var store_street1 = 'Sesamy street';//Set constant for testing
    var store_zipcode = '0574'; //Set constant for testing
    var dataString = 'name='+ store_name + '&street1=' + store_street1 + '&zipcode=' + store_zipcode;    
    jQuery.ajax(
    {   
      type: "POST",   
      url: "process_frm_store.php",   
      data: dataString,
      error: function(XMLHttpRequest, textStatus, errorThrown) 
      { 
        alert(errorThrown); // Just for debugging
        jQuery('#suggestNewStore div.error').fadeIn(); 
      },
      success: function() 
      {
        alert('It works!');
        jQuery('#suggestNewStore div.success').fadeIn();   
      }   
    });   
    return false;      
  });
}

【问题讨论】:

    标签: php jquery mysql wordpress


    【解决方案1】:

    使用像http://www.yourwebsite.com/storelocator/process_frm_store.php 这样的绝对路径更安全。

    否则 .ajax 方法的 url 参数应该是 storelocator/process_frm_store.php,因为您的 ..js 文件包含在您的基本路径中并在 jsstorelocator 文件夹之外执行

    【讨论】:

    【解决方案2】:

    &lt;?php 标签包裹在 sl_register.tpl.php 中的 include_once 中,而它已经在 &lt;?php 标签 ...除非你正在做我不明白的事情,否则这可能会破坏一些东西。

    【讨论】:

    • 哦,对不起...这只是我的剪切和粘贴中的一个错字。在我的完整代码中,该行位于 PHP 标记之外。我会编辑和解决这个问题。
    猜你喜欢
    • 2015-01-27
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    相关资源
    最近更新 更多