I am working on a feature that involves posting to ajax. The relevant bit of code is supposed to pick out a certain class, loop through its elements, put the desired elements in an array and post that array. It works fine for getting background image links. However, the same concept seems to fail when applied to text written in an html textarea. Even though I use the .val method to get the text from the textarea and push it into the relevant array, which I then post, no text gets uploaded to the database. Here is javascript.
$('#createButton').click(function() {
var getImages = [];
var getText = [];
var count = $('.count').length;
var tcount = $('.tcount').length;
if ( count >= 1 ) {
$('.count').each(function () {
if ($(this).css('background-image')) {
var src = $(this).css('background-image');
var parseSrc = src.replace(/^.*(?=images)|\W+$/g, ''); //parse out the actual link from url( link )
getImages.push(parseSrc);
}//end nested if
});//end each function
}//end count if
if ( tcount >= 1 ) {
$('.tcount').each(function () {
if ($(this).val()) {
var textVal = $(this).val();
getText.push(textVal);
}//end if
});//end each
}//end tcount if
$.post('fileHandler.php', {image: getImages, text: getText});//end post
});//end createButton click
And the PHP
include('classes/fileclass.php');
$f = new Files ();
$storyID = $f->idHandler();
if ($_POST['image']) {
foreach($_POST['image'] as $imageSrc) {
$f->insertStoryImages($storyID, $imageSrc);
}//end foreach
if ($_POST['text']) {
foreach($_POST['text'] as $textBox) {
$f->insertStoryText($storyID, $textBox);
}//end foreach
}//end text if
}//end image if
And the bit that actually does the server call from the OOP script (for the text call only, the image works just fine):
public function insertStoryText ($storyID, $textBox) {
$db = new pdo("mysql:host=localhost;dbname=storyboard;charset=utf8", "someuser", "somepass");
$stmt = $db->prepare("INSERT INTO sbData(storyID, textBox) VALUES (:storyID, :textBox)");
$stmt->execute(array(':storyID' => $storyID, ':textbox' => $textBox));
}//end insertShoot
NOTE: To be clear, the .tcount
class picks out an html textarea object.