4 Replies - 526 Views - Last Post: 13 February 2015 - 11:16 AM

#1 MOUDA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 15

Place json into modal

Posted 11 February 2015 - 11:16 PM

I have a JSON returned data from a pho function that looks as follows:

[{"id":"15","activity_type":"call","activity_title":"Call to check "}] 


Here the script that initiates the request (actvitiy.js)(Edited)
$(document).on("click", ".view_contact_activity", function () {

        var this_activity_id = $(this).closest('.feeds').find('#this_activity_id').val();
        $('#view-contact-activity').modal('show');
    $.ajax({

        url: '../includes/functions/contact-functions.php',
        data: {view_activity_id:this_activity_id},
        dataType:'json',
        Success: function(response){
        $('#activity_id').val(response[0].id);
        $('#activity_type').val(response[0].activity_type);

        }
            });
});


The modal where i need the values to show:
<div class="modal fade" id="view-contact-activity" tabindex="-1" role="basic" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
             <div class="modal-body">
              <div class="portlet-body form">
              <form class="form-horizontal" role="form" id="view-contact-activity-form" method="post">

                  <div class="form-group">
                   <label class="col-sm-3 control-label col-lg-3"> Activity Title</label>
                      <input type="text" name="activity_id" id="activity_id" value="">
                   <label class="col-sm-3 control-label col-lg-3"> Activity Type</label>
                        <input type="text" name="activity_type" id="activity_type" value="">                                                
                  <div class="modal-footer">
                      <div class="col-lg-offset-2 col-lg-10">
                          <button type="submit" name="create-new-account" class="btn btn-danger" id="edit">Edit Activity</button>
                      </div>
                  </div>
              </form>
          </div>
          </div>
        </div>
    </div>
</div>



PHP function that returns the JSON
function view_activity(){

        global $connection;

        $activity_id = $_POST['view_activity_id'];
        $get = "SELECT * FROM contact_activities WHERE activity_id = '$activity_id' "
                                    or die("Error: ".mysqli_error($connection));
        $query = mysqli_query($connection, $get);
        $activitiy_field = array();

        while ($activity_array = mysqli_fetch_array($query)){

            $activity = array(
                'id' => $activity_array['activity_id'],
                'activity_type' => $activity_array['activity_type'],
                'activity_title'=>$activity_array['activity_title'],
                'activity_details'=>$activity_array['activity_details'],
                'activity_status'=>$activity_array['activity_status'],
                'activity_details'=>$activity_array['activity_details'],
                'activity_details'=>$activity_array['activity_details'],

                );

        $activitiy_field[] = $activity;

        }
                echo json_encode($activitiy_field);
    }

    if (isset($_POST['view_activity_id'])) {
        view_activity();
    }

The problem: The modal shows but no data is passed into the modal. Any ideas what i might be doing wrong here.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Place json into modal

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Place json into modal

Posted 12 February 2015 - 05:08 AM

Moving to jQuery, as this doesn't look like a PHP problem.

Are you getting any errors in the Javascript console? Have you used the browser development tools to place a breakpoint inside the Success closure (which I think should be success with a lower-case s) to ensure you're getting back what you think you should be?
Was This Post Helpful? 0
  • +
  • -

#3 MOUDA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 15

Re: Place json into modal

Posted 12 February 2015 - 08:40 AM

View PostJackOfAllTrades, on 12 February 2015 - 05:08 AM, said:

Moving to jQuery, as this doesn't look like a PHP problem.

Are you getting any errors in the Javascript console? Have you used the browser development tools to place a breakpoint inside the Success closure (which I think should be success with a lower-case s) to ensure you're getting back what you think you should be?


Thanks for moving the post. no errors are happening. firebug reports the array fine in response and in HTML tabs. i also added
 console.log($("#activity_id").length); 
to see if the id is found more than once, but it was only found once.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Place json into modal

Posted 12 February 2015 - 06:25 PM

Is this bootstrap's modal? If so, you might try something like this (not tested, not a Bootstrap or UI expert):

$(document).on("click", ".view_contact_activity", function () {

    var this_activity_id = $(this).closest('.feeds').find('#this_activity_id').val();
    $('#view-contact-activity').modal('show')
    .on('show.bs.modal', function(event) { // event triggered after show is triggered
        $modal = $(this); // for use in find for ajax callback 
        $.ajax({
           url: '../includes/functions/contact-functions.php',
           data: {view_activity_id:this_activity_id},
           dataType:'json',
           success: function(response){
               // Find the elements in the modal
               $modal.find('#activity_id').val(response[0].id);
               $modal.find('#activity_type').val(response[0].activity_type);
           }
        });
    });
});

Was This Post Helpful? 0
  • +
  • -

#5 MOUDA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 15

Re: Place json into modal

Posted 13 February 2015 - 11:16 AM

Great, Thank you. got it to work this way. slight modification:
$('#view-contact-activity').on('show.bs.modal', function () 

instead of
 .on('show.bs.modal', function(event)

This post has been edited by andrewsw: 13 February 2015 - 11:18 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1