0

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.

esqew
  • 42,425
  • 27
  • 92
  • 132
nmac
  • 610
  • 1
  • 10
  • 20
  • First of all you have to determine where the error is. I suggested to write in log or console or whatever the content of $_POST['text'] at the very begining of the PHP part. That way you can centrate in PHP or in javascript part. – EduSanCon Jul 10 '14 at 20:24
  • 1
    If you watch your browser's javascript console (at least for Chrome or Firefox), you should see the AJAX call executed... does that have the data structured how you expect? If so, the issue is server-side. If not, its client-side. – Brian Jul 10 '14 at 20:57

1 Answers1

0

The issue was that '.tcount' did not denote a textarea but a div. So naturally, when I used .val() it would return an empty string. Terrible bug.

nmac
  • 610
  • 1
  • 10
  • 20