嘿伙计们!
目的是让用户能够在模拟整个季后赛的每对中挑选优胜者。一旦他完成了模拟,表格就出现在底部:
我无法设法工作的方法是让表单不仅传递Imie
/ Email
字段(这相当简单),而且还包括整个括号以便我可以检查括号发送和比较。
我听说ajax
在这种情况下会有很大的帮助,所以我根据我在网上发现的一些信息编写了这个脚本(我是ajax的新手):
$(document).ready(function() {
$('form').submit(function(event) {
var data = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
'formularz' : $('#formdiv').val()
};
$.ajax({
type : 'POST',
url : 'process.php',
data : data,
dataType : 'json',
encode : true
})
.done(function(data) {
console.log(data);
});
event.preventDefault();
});
});
Process.php
如下所示:
<?php
$errors = array();
$data = array();
if (empty($_POST['name']))
$errors['name'] = 'Imie jest wymagane.';
if (empty($_POST['email']))
$errors['email'] = 'Email jest wymagany.';
if (empty($_POST['formularz']))
$errors['formularz'] = 'Uzupełnij formularz.';
if ( ! empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
} else {
var imie = data.name;
alert(imie);
$data['success'] = true;
$data['message'] = 'Success!';
}
echo json_encode($data);
从ajax代码,我假设我应该获得带有表单数据的console.log,但是没有日志。表单数据是否可能因为存储了大的div并且由于它而失败?
非常感谢任何帮助!
以下是完整代码和HTML的链接:
html代码示例:
<div id="formdiv">
<div class="col-cell">
<div class="row" id="team1">
<?php
$team1 = "SELECT id, timestamp, teamname, teamtag FROM teams WHERE id = '1'";
$result = mysqli_query($con, $team1);
if (mysqli_num_rows($result) == 0) {
echo '<div class="team-block">';
echo '<img src="img/basketball.png"/>';
echo '<div class="team-tag">';
echo "<h2>---</h2>";
echo '</div></div>';
}
else
{;
while(list($id, $timestamp, $teamname, $teamtag) = mysqli_fetch_row($result))
{
$idfromsql = "SELECT * FROM teams WHERE id = $id";
$sthi = $con->query($idfromsql);
$imgresult = mysqli_fetch_array($sthi);
echo '<div class="team-block">';
echo '<div id="team111"><img src="data:image/jpeg;base64,'.base64_encode( $imgresult['data'] ).'"/></div>';
echo '<div class="team-tag">';
echo "<h2>$teamtag</h2>";
echo '</div></div>';
}
};
?>
<div class="numleft">1</div>
</div>
<div class="row" id="team2">
<?php
$team2 = "SELECT id, timestamp, teamname, teamtag FROM teams WHERE id = '2'";
$result = mysqli_query($con, $team2);
if (mysqli_num_rows($result) == 0) {
echo '<div class="team-block">';
echo '<img src="img/basketball.png"/>';
echo '<div class="team-tag">';
echo "<h2>---</h2>";
echo '</div></div>';
}
else
{;
while(list($id, $timestamp, $teamname, $teamtag) = mysqli_fetch_row($result))
{
$idfromsql = "SELECT * FROM teams WHERE id = $id";
$sthi = $con->query($idfromsql);
$imgresult = mysqli_fetch_array($sthi);
echo '<div class="team-block" onclick="adv1();">';
echo '<div id="team211"><img src="data:image/jpeg;base64,'.base64_encode( $imgresult['data'] ).'"/></div>';
echo '<div class="team-tag">';
echo "<h2>$teamtag</h2>";
echo '</div></div>';
}
};
?>
</div>
</div>
</div>
答案 0 :(得分:1)
var data = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
'formularz' : $('#formdiv').val()
};
formularz为空,因为.val()函数仅适用于输入字段。 如果您确实要传输完整的div,请使用.html()或.text()。
更简单的方法,根据所提出的解决方案使用隐藏的输入。