4

Essentially what I'm trying to figure out is how to retrieve the value a span(id="camera_status") that's been populated with the name of a image after its been captured using phonegap.

The way I want it to work is that the image name will then be uploaded to the database along with the form information that's submitted. At the moment it seems to be reading the initial blank value before the span is populated with the image name. If there's anyone who know's how I can fix this i would be grateful.

HTML and Javascript of form page

<div>
  <input type="button" onclick="takePicture();" value="Take Picture" /><br/>
</div>
<div>
<div>
    <b>Status:</b> <span id="camera_status"></span><br>
    <b>Image:</b> <img style="width:240px;visibility:hidden;display:none;" id="camera_image" src="" />
</div>      
<div id="landmark-1" data-landmark-id="1">
<form id="uploadForm">
    <label for="email">
        <b>Email</b>
        <input type="email" id="email" name="email">
    </label>

    <label for="comment">
        <b>Comment</b></br>
        <input id="comment" name="comment" cols="30" rows="10"></textarea>
    </label>
    <input type="button" onclick="uploadPicture();" value="Upload New location" />  
    <input type="submit" value="Upload New location" />

    </form>

<script>
var spanValue = $('#camera_status').html()

$(function(){

$('#uploadForm').submit(function(){
    var landmarkID = $(this).parent().attr('data-landmark-id');
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData+'&lid='+spanValue,
        //change the url for your project
        url: 'save.php',
        success: function(data){
            console.log(data);
            alert('Your comment was successfully added');
        },
        error: function(){
            console.log(data);
            alert('There was an error adding your comment');
        }
    });

    return false;
 });
});

</div>
</div>
</div>
1
  • Wrap all of this in document.ready Commented May 3, 2013 at 18:23

2 Answers 2

7

Try .text() :

var spanValue = $('#camera_status').text();
Sign up to request clarification or add additional context in comments.

Comments

3

Get the spanValue when you're actually going to use it, and not outside the DOM ready function, before anything is loaded :

$(function(){
    $('#uploadForm').on('submit', function(e){
        e.preventDefault();
        var landmarkID = $(this).parent().data ('landmark-id'),
            postData   = $(this).serialize(),
            spanValue  = $('#camera_status').text()    

        $.ajax({
            type: 'POST',
            data: postData+'&lid='+spanValue,
            url: 'save.php'
        }).done(function(data) {
            console.log(data);
        });
     });
});

1 Comment

Thank you so much! Knew it had to be something simple but just couldn't figure it out, cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.