希望有人可以帮助您使用此代码。我有一个带有foreach循环的php文件,用于从mysql数据库中获取数据。当用户单击php文件中的Update时,将调用此JS脚本。我正在使用"这个"获得一组单独的值。我遇到的问题是没有任何内容传递给JS脚本。
这是PHP代码,PHP代码的工作原理是使用echo语句进行测试。
<?php
$artist_about_already = artist_news_check($artist_id);
if ($artist_about_already == true) {
$artist_news = (get_artist_news_all($artist_id));
foreach ($artist_news as $news) {
$news_id = $news['id'];
$timestamp = $news['timestamp'];
$title = $news['title'];
$news = $news['news'];
$news_timestamp = date('D M d Y @ h:i:s', $timestamp);
?>
<div class="well no-padding">
<form action="" method="post" name="artist_update_news" id="artist_update_news" class="smart- form client-form">
<fieldset>
<section>
<strong>Posted on: <?php echo $news_timestamp ?></strong>
<label class="input"> <i class="icon-append fa fa-user"></i>
<input type="text" name="artist_update_news_title" id="artist_update_news_title" value="<?php echo $title ?>">
<b class="tooltip tooltip-bottom-right">Needed to update Artist News</b> </label>
</section>
<section>
<label class="textarea">
<textarea rows="5" maxlength="2000" name="artist_edit_news" id="artist_edit_news"><?php echo $news ?></textarea>
</label>
</section>
<input type="hidden" name="hiddenId" id ="hiddenId" value="<?php echo $news_id; ?>">
</fieldset>
<footer>
<button type="submit" class="btn btn-success updateBtn" href="#artist_news.php" id="<?php echo $id ?>">
Update
</button>
</footer>
</form>
</div>
<?php
}
}
?>
当用户在foreach语句中的任何单个记录上单击“更新”时,将调用以下代码。
runAllForms();
$(function () {
$("#artist_update_news").validate({
rules: {
artist_update_news_title : {
required : true
},
artist_edit_news : {
required : true
}
},
// messages for form validation
messages : {
artist_update_news_title : {
required : 'Please enter your News Heading'
},
artist_edit_news : {
required : 'Please enter your News'
}
},
errorPlacement : function(error, element) {
error.insertAfter(element.parent());
},
submitHandler: function() {
alert('in function');
var element = $(this);
var hiddenId = element.attr("hiddenId");
var artist_edit_news = element.attr("artist_edit_news");
var artist_update_news_title = element.attr("artist_update_news_title");
alert(hiddenId);
alert(artist_edit_news);
alert(artist_update_news_title);
$('#artist_update_news').hide(0);
$('#art_edit_news_message').hide(0);
$.ajax({
url : 'artist_edit_news.php',
type : 'POST',
dataType : 'json',
data: {
artist_update_news_title : artist_update_news_title.val(),
artist_edit_news : artist_edit_news.val(),
hiddenId : hiddenId.val()
},
success : function(data){
$('#art_edit_news_message').removeClass().addClass((data.error === true) ? 'error' : 'success')
.text(data.msg).show(500);
if (data.error === true) {
if (data.goto == 1) {
window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_news.php";
$('#artist_update_news').show(500);
delete json;
}
else {
$('#artist_update_news').show(500);
}
}
if (data.error === false) {
window.location = "http://dev.worldmusicstage.com/main.php#/ajax/artist_news.php";
$('#artist_update_news').show(500);
delete json;
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$('#art_edit_news_message').removeClass().addClass('error')
alert('The error was: '+errorThrown);
alert('The error was: '+XMLHttpRequest);
alert('The error was: '+textStatus);
alert(errorThrown.stack)
$('#artist_update_news').show(500);
}
});
return false;
}
});
});
答案 0 :(得分:0)
您需要在
中为输入标记生成唯一的动态IDid ="hiddenId"
我们还需要在
中设置变量 $ id 的值<button
type="submit"
class="btn btn-success updateBtn"
href="#artist_news.php"
id="<?php echo $id ?>"
>
您需要为输入标记生成唯一的动态ID,或者在
中使用类选择器$("#artist_update_news").validate({
并在
<form action=""
method="post"
name="artist_update_news"
id="artist_update_news"
class="smart-form client-form">