gpt4 book ai didi

javascript - jQuery XMLHttpRequest 调用外部 PHP 表单未提交

转载 作者:行者123 更新时间:2023-11-28 08:29:12 24 4
gpt4 key购买 nike

我最近有一位专门研究梯形逻辑而不是网络编程的 friend 向我请求她雇主的一个项目帮助。虽然我使用更传统的编码语言,但我自己远不是 jquery 和 php 方面的专家。我们遇到的问题是,带有 jquery/html 表单的 php 页面通过 XMLHttpRequest 插入到父页面中,没有从父页面执行其“post”操作。使这个问题变得更加困难的是,当页面在父页面之外自行运行(直接加载到浏览器中)时,它会很好地执行其“post”操作。我已经做了很多小时的搜索和反复试验,但很困惑,现在向您寻求帮助。下面是相关的代码。我们将非常感谢您提供的任何帮助,因为在通过 XMLHttpRequest 插入表单时执行表单提交时,我们尝试过的任何方法似乎都不起作用。

来自父页面的Javascript代码插入外部表单:

function showUser(str)
{
if (str=="")
{
document.getElementById("insertUserHere").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp.status==200)
{
document.getElementById("insertUserHere").innerHTML=xmlhttp2.responseText;
}
}

xmlhttp2.open("GET","ajax-userForm.php?q="+str,true);
xmlhttp2.send();
}

由xhmlhttprequest插入的外部PHP页面代码(ajax-userForm.php):

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">

// JQUERY: Plugin "autoSubmit"
(function($) {
$.fn.autoSubmit = function(options) {
return $.each(this, function() {
// VARIABLES: Input-specific
var input = $(this);
var column = input.attr('name');

// VARIABLES: Form-specific
var form = input.parents('form');
var method = form.attr('method');
var action = form.attr('action');

// VARIABLES: Where to update in database
var where_val = form.find('#where').val();
var where_col = form.find('#where').attr('name');


// ONBLUR: Dynamic value send through Ajax
input.bind('blur', function(event) {



// Get latest value
var value = input.val();

if (input.attr('type') == "checkbox")
{

if (input.attr('checked') )
{
value = 1;
}
else
{
value = 0;
}

}


// AJAX: Send values
$.ajax({
url: action,
type: method,
data: {
val: value,
col: column,
w_col: where_col,
w_val: where_val

},
cache: false,
timeout: 10000,
success: function(data) {
// Alert if update failed
if (data) {
alert(data);
}
// Load output into a P
else {
$('#notice').text('Updated');
$('#notice').fadeOut().fadeIn();
}
}
});
// Prevent normal submission of form
return false;
})
});

}
})(jQuery);
// JQUERY: Run .autoSubmit() on all INPUT fields within form
$(function(){
$('#ajax-userForm INPUT').autoSubmit();
});

</script>
<!-- STYLE -->
<style type="text/css">
INPUT { margin-right: 1em }
</style>


</head>
<body>

<!-- CONTENT -->
<?php
$q = intval($_GET['q']);
/*
* DATABASE CONNECTION
*/

// DATABASE: Connection variables
$db_host = "localhost";
$db_name = "DBNAME";
$db_username = "root";
$db_password = "DBPWD";

// DATABASE: Try to connect
if (!$db_connect = mysql_connect($db_host, $db_username, $db_password))
die('Unable to connect to MySQL from ajax-form.');
if (!$db_select = mysql_select_db($db_name, $db_connect))
die('Unable to select database');

/*
* DATABASE QUERY
*/

// DATABASE: Get current row
//$result = mysql_query("SELECT * FROM user");
$result = mysql_query("SELECT * FROM user where Project_ID = '".$q."' ");
$row = mysql_fetch_assoc($result);



?>
<form id="ajax-userForm" class="autosubmit" method="post" action="ajax-updateUser.php">
<fieldset>
<legend>Update user information</legend>

<label>First Name:</label>
<input name="FirstName" value="<?php echo $row['FirstName'] ?>" />

<label>Last Name:</label>
<input name="LastName" value="<?php echo $row['LastName'] ?>" />

<label>Hometown</label>
<input name="Hometown" value="<?php echo $row['Hometown'] ?>" />

<label>Married</label>
<input type = "checkbox" id = "chkMarried" name="Married" <?php echo $row['Married'] == 1 ? 'checked':'unchecked' ?>/>

<label>Employed</label>
<input type = "checkbox" id = "chkEmployed" name="Employed" <?php echo $row['Employed'] == 1 ? 'checked':'unchecked' ?> />

<input id="where" type="hidden" name="Project_ID" value="<?php echo $row['Project_ID'] ?>" />
</fieldset>
</form>

<p id="notice"></p>


</body>
</html>

页面代码(ajax-updateUser.php)由上面代码中的“post”操作调用(ajax-userForm.php):

/*
* DATABASE CONNECTION
*/

// DATABASE: Connection variables
$db_host = "localhost";
$db_name = "DBNAME";
$db_username = "root";
$db_password = "DBPWD";

// DATABASE: Try to connect
if (!$db_connect = mysql_connect($db_host, $db_username, $db_password))
die('Unable to connect to MySQL from ajax-update.');
if (!$db_select = mysql_select_db($db_name, $db_connect))
die('Unable to select database');
$message = "Connection Successful";
//echo "<script type='text/javascript'>alert('$message');</script>";
// DATABASE: Clean data before use
function clean($value)
{
return mysql_real_escape_string($value);
}

/*
* FORM PARSING
*/

// FORM: Variables were posted
if (count($_POST) > 0)
{
$message = count($_POST);
//echo "<script type='text/javascript'>alert('$message');</script>";
// Prepare form variables for database
foreach($_POST as $column => $value)
${$column} = clean($value);

// Perform MySQL UPDATE
$result = mysql_query("UPDATE user SET ".$col."='".$val."'
WHERE ".$w_col."='".$w_val."'")
or die ('Unable to update row.');
}
else
{
$message = "Nothing in Post";
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>

最佳答案

几件事:

缺少对您的报价

DBPWD

您对状态 200 的检查用途:

xmlhttp // whereas the rest is xmlhttp2

我的理论,没有更多背景 -声明时没有使用 var 关键字:

xmlhttp2=new XMLHttpRequest();

这意味着请求像这样附加到窗口:window.xmlhttp2 = ... - 您是否会意外修改“父”页面上其他位置的相同标识符?这可以解释共享状态错误以及为什么它只能单独工作(您不会有其他代码隐式修改 window.xmlhttp2)

您也可能会做坏事:

xmlhttp2.open("GET","ajax-userForm.php?q="+str,true);

因为我不知道这条路意味着什么。

另一个可能是,您是否为来自外部域的请求设置了 CORS header ?

干杯,
安德鲁

关于javascript - jQuery XMLHttpRequest 调用外部 PHP 表单未提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com