【发布时间】:2017-02-07 12:37:44
【问题描述】:
我在 stackoverflow 中搜索了这个问题,发现的答案很少,但没有一个对我有帮助。所以我再次发布它。我在本地主机中使用 post 方法的 php 表单工作正常,但相同的代码在服务器(linux 服务器)中不起作用我不知道实际问题是什么。请帮助
- 我要提交的表格
<form class="forma" id="forma" method="POST" action="contact-form.php" name="myForm" autocomplete="off">
<div class="formWrapper">
<div class="group">
<input type="text" name="name" id="name">
<span class="highlight"></span>
<span class="bar"></span>
<label>Name</label>
<p class="error-red"></p>
</div>
<div class="group">
<input type="text" name="eMail" id="eMail">
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
<p class="error-red"></p>
</div>
<div class="group">
<input type="text" name="mobileNo" id="mobileNo">
<span class="highlight"></span>
<span class="bar"></span>
<label>Mobile no.</label>
<p class="error-red"></p>
</div>
<div class="group">
<input type="text" name="message" id="message">
<span class="highlight"></span>
<span class="bar"></span>
<label>Message</label>
</div>
<div class="captchaWrapper">
<img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimgCont" style="margin-right: 14px;" alt="captcha">
<a href="javascript: refreshCaptcha();" class="clickTorefreshAnchor">
<i class="fa fa-refresh clickTorefresh" id="caprefresh">
</i>
</a>
<div class="captchaInputWrapper group">
<input type="text" id="6_letters_code" name="captcha">
<span class="highlight"></span>
<span class="bar"></span>
<label>Enter the code here:</label>
<p class="error-red"></p>
</div>
</div>
<div class="submit submitBtn" id="consubmit">
<button type="submit" name="sendmail" id="sendmail" class="sendMailBtn">Submit
</button>
</div>
</div>
<div class="clear"></div>
</form>
- 我的 jquery ajax 代码
jQuery(document).ready(function() {
jQuery("#forma").submit(function(e) {
e.preventDefault();
e.stopImmediatePropagation();
jQuery('#sendmail').html('Please wait...');
jQuery.ajax({
type: "post",
url: './contact-form.php',
data: jQuery("#forma").serialize(),
dataType: "json",
success: function(data) {
jQuery('p.error-red').html('');
if ('success' == data.result) {
//jQuery("#forma")[0].reset();
//jQuery('#response').append('<p class="successMessage">' + data.msg + '</p>');
// jQuery('#sendmail').html('Submit');
window.location.href = "https://www.mrpmediaworks.com/thanks.php";
}
if ('error' == data.result) {
var arr = data.msg;
var element_arr = data.fieldname;
count = '0';
jQuery.each(arr, function(index, value) {
if (value.length != 0) {
jQuery('input[name=' + element_arr[count] + ']').parent('.group').find('p.error-red').html(value);
}
count++;
});
jQuery('#sendmail').html('Submit');
}
}
});
return false;
});
});
- 我的php代码
<?php
session_start();
require_once('config.php');
$email = '';
$fieldname = array();
$msg = array();
$result = '';
$count = '';
$name = $_Post["name"];
$contact = $_Post["mobileNo"];
$email = $_Post["eMail"];
$msg_query = $_Post["message"];
$captcha = $_Post["captcha"];
echo $name;
die();
?>
只是测试代码
相同的代码适用于 get
我已尝试从表单中删除操作,并可能进行组合,但注意到有效。 编辑后
4. .htaccess file
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
这是我的 htaccess 文件中唯一的内容
【问题讨论】:
-
你的表单在哪里,我在你的代码中看不到。
-
@AnkitShah 并不是真正的重复。他需要返回具有 data.result 属性的 json
-
我没有发布完整的 php 代码,但它有 echo json_encode($result);但它不工作。我想说我无法将数据从表单传递到此处命名为 contact-form.php 的 php 页面。
标签: php jquery ajax post server