0

I am trying to save some data to a database via PHP and jQuery but after the 3rd index, the 3rd index is used instead of the 4th index and 5th index is not used entirely. I've tested the data being sent via jQuery and it all lines up perfectly but the PHP part is repeating the 3rd index value twice.

Here is my code:

jQuery/JavaScript:

// get the creating profile data function getProfileData(element, button) { var profile_data = $(element).html();

var data = $(element).blur(function() {
    profile_data = $(element).map(function() {
        return this.innerHTML;
    }).get();

});

$(button).on('click', function() {
    var edited_content = profile_data.join(", ");

    var split = edited_content.split(", ");

    $.ajax({
        method : "POST",
        url : "/members/profile/create-profile",
        data : {
            display_name : split[0],
            email_address : split[1],
            age : split[2],
            location : split[3],
            bio : split[4]
        },
    }).done(function() {
        alert("Profile saved!")
    }).fail(function() {
        alert("Error saving profile, please try again");
    });
});

}

and here is the PHP part (using zend framework 2)

 public function createprofileAction()
 {
    if ($this->getProfileService()->checkIfProfileSet()) {
        return $this->redirect()->toRoute('members/profile', array('action' => 'index'));
    }



    if ($this->request->isPost()) {
        $params = $this->params()->fromPost();


        $this->getProfileService()->makeProfile(array(
            'display_name'  => $params['display_name'],
            'email_address' => $params['email_address'],
            'age'           => $params['age'],
            'location'      => $params['location'],
            'bio'           => $params['bio'],
        ));
    }
}

html:

<div class="w3-card-2 w3-round">
    <div class="w3-accordion w3-white">
        <button onclick="expand('side-1')"
            class="w3-btn-block w3-theme-l1 w3-left-align">
            <i class="fa fa-edit fa-fw w3-margin-right"></i> Display Name
        </button>

        <div id="side-1" class="w3-accordion-content w3-container">
            <p contenteditable="true">Click to set your display name</p>
        </div>

        <button onclick="expand('side-2')"
            class="w3-btn-block w3-theme-l1 w3-left-align">
            <i class="fa fa-edit fa-fw w3-margin-right"></i> Email Address
        </button>

        <div id="side-2" class="w3-accordion-content w3-container">
            <p contenteditable="true">Click to set your email address</p>
        </div>

        <button onclick="expand('side-3')"
            class="w3-btn-block w3-theme-l1 w3-left-align">
            <i class="fa fa-edit fa-fw w3-margin-right"></i> My Age
        </button>

        <div id="side-3" class="w3-accordion-content w3-container">
            <p contenteditable="true">Click to set your age</p>
        </div>

        <button onclick="expand('side-4')"
            class="w3-btn-block w3-theme-l1 w3-left-align">
            <i class="fa fa-edit fa-fw w3-margin-right"></i> My Location
        </button>

        <div id="side-4" class="w3-accordion-content w3-container">
            <p contenteditable="true">Click to set your location</p>
        </div>

        <button onclick="expand('side-5')"
            class="w3-btn-block w3-theme-l1 w3-left-align">
            <i class="fa fa-edit fa-fw w3-margin-right"></i> Bio
        </button>

        <div id="side-5" class="w3-accordion-content w3-container">
            <p contenteditable="true">Click to set your Bio</p>
        </div>
    </div>
</div>

<br><br>

<div class="w3-display-container">
    <div class="w3-display-bottomright">
        <button class="w3-btn-block w3-theme-l1" id="save">Save</button>
    </div>
</div>

<script type="text/javascript">
    getProfileData('[contenteditable]', $('#save'));
</script>

To better explain it, age is inserted but then it fills in the location field in the database table and bio takes the location value.

I can provide more information if need be.

Appreciate any help!

Thanks

user2101411
  • 1,204
  • 2
  • 14
  • 33
  • This code doesn't make sense: `if (element != $(this).html())` what is it supposed to do? – Mottie Jan 07 '17 at 14:20
  • Why are you joining, splitting and exploding the data like that? How is the data updated/edited in your html? Seems kind of redundant to first join the data just to split it right after? Btw, if any of the fields contains a comma, it will mess everything up. – M. Eriksson Jan 07 '17 at 14:22
  • it is checking to see if the element contains a new value instead of the old value. the data is updated in my html via contenteditable fields. – user2101411 Jan 07 '17 at 14:25
  • 1
    Still does not make sense to make such comparison: `element` seems to be a jQuery selector, and there is no reason why it should ever equal `.html()` of some DOM element. – trincot Jan 07 '17 at 14:29
  • It is getting the data inside the element, thus html() is used. It's working on the jquery side, I've alerted all the data and it shows up fine. The php side however is a different story, as it uses age twice. – user2101411 Jan 07 '17 at 14:30
  • I don't think you understand what I am saying. – trincot Jan 07 '17 at 14:31
  • how is this a duplicate question? – user2101411 Jan 07 '17 at 14:33
  • In your code, the variable `element` contains the literal string `[contenteditable]`, so your code is equal to: `if ('[contenteditable]' != $('[contenteditable]').html())`, which, both @trincot and @Mottie already pointed out, doesn't make any sense. – M. Eriksson Jan 07 '17 at 14:37
  • @MagnusEriksson weather it makes sense or not..even if you remove that faulty logic line...it will not a change a thing...OP is telling his problem is with PHP and that ambiguous condition is no reason for it – Nishanth Matha Jan 07 '17 at 14:41
  • I changed that just now. – user2101411 Jan 07 '17 at 14:41
  • what exactly is wrong with my php? – user2101411 Jan 07 '17 at 14:42
  • @NishanthMatha - I was just explaining what the other commenters pointed out, which he didn't really understand. – M. Eriksson Jan 07 '17 at 14:45
  • nevermind, I fixed it. sorry for wasting your guys' time :( – user2101411 Jan 07 '17 at 14:46
  • actually, how would i go about allowing for commas to be used without having the data being erased? For instance, .split(",") cuts off the string when it finds a comma. I am trying to get all the fields into an array via .map method, and then split up the string into an array. But like @MagnusEriksson said, the field that contains a comma will screw it all up. How would I fix this? – user2101411 Jan 07 '17 at 16:08

0 Answers0