-1
누군가가이 코드를 사용하면 도움이 될 수 있습니다. MySQL 데이터베이스에서 데이터를 가져 오는 foreach 루프가있는 PHP 파일이 있습니다. 사용자가 php 파일에서 Update를 클릭하면이 JS 스크립트가 호출됩니다. 개별 값 집합을 얻으려면 "this"를 사용합니다. 내가 겪고있는 문제는 JS 스크립트에 전달되는 것이 아무것도 없다는 것입니다.PHP에서 Ajax로 Foreach 데이터 전달
여기에 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;
}
});
});
감사의 , 내가 제출 버튼에 대한 개정을했다. ID가 잘못되어 $ news_id로 업데이트했습니다. 그러나 여전히 작동하지 않습니다. \t \t \t \t \t \t \t <버튼 타입 = 클래스 = HREF = "#의 artist_news.php"ID = "BTN-성공 updateBtn btn을" " $ news_id?> 에코 PHP", "제출"> –
감사 케빈. 나는 이것을 어떻게하는지 혼란 스럽다. 양식 ID에 고유 ID를 지정하면 어떻게 그 스크립트에서 해당 ID를 지정해야합니까? foreach를 반복하면서 artist_update_news 대신 고유 ID에 1, 2, 3, 4 등을 할당 할 수 있지만 어떻게 스크립트에서 식별 할 수 있습니까? 또는 getelementbyid로 참조 할 수 있습니까? 당신의 도움을 주셔서 감사합니다. 부여 –