Jump to content

Using x-editables to save fields


h4r4ld
 Share

Recommended Posts

Hi,

I'm want to save my userfields on a userprofile frontpage with x-editables.

Therefor I have a field in my userprofile page like :

<a href="#" id="web" data-type="url" data-pk="1" data-title="Enter website"><?=$user->web?></a>

onClick I get a popup with an inputfield, which works pretty nice, the javascriptcode is like :

    $('#web').editable({
        type: 'text',
        pk: 1,
        name: 'web',
        url: 'http://pw.mysite.de/site/templates/assets/requests/handleProfileFields.php',
        title: 'Enter website'
    });

So if the user submits his changes, the handleProfileFields.php should be called which is like : 

$field = $_POST['name'];
$value = $_POST['value'];

if(!empty($value)) {

    $user->$field = $value;
    $user->save();

} else {

    header('HTTP 400 Bad Request', true, 400);
    echo "This field is required!";
}

My problem is that as a *.php file I get a HTTP 404, renamed to *.txt I can open it. I guess it has something to do with pagerendering of process wire, so im aware if I put my assets to the right place or if I should put it somewhere else to be included as are.

Also I'm wondering if this is the best way to save the data to database and if $user-save() will be available at this point?

Best,

Harri

Link to comment
Share on other sites

Maybe this can help you to start off?

That helped, thanks.

If anyone else is faceing a similar problem, here is what came out changed:

echo '<a href="#" id="web" data-type="url" data-pk="'.$user->id.'_url" data-title="Enter website">'.$user->web.'</a>';

Since I change fields which are in the $user page I need to bring the userid, also I need to know which type of field im handling to sanitize it. 

This is done by the pk field, where I connect the ID with _url, _email, _text or _textarea (not all sanitizes covered yet).

// handleProfileFields.php

    include("./index.php");

    $field = $_POST['name'];
    $value = $_POST['value'];
    $pk = explode('_',$_POST['pk']);

    $item = $wire->users->get($pk[0]);

    if(!empty($value)) {

        $item->setOutputFormatting(false);

        switch ($pk[1]) {
            case 'url':
                $item->$field = $sanitizer->url($value);
                $item->save();
                break;
            case 'email':
                $item->$field = $sanitizer->email($value);
                $item->save();
                break;
            case 'text':
                $item->$field = $sanitizer->text($value);
                $item->save();
                break;
            case 'textarea':
                $item->$field = $sanitizer->textarea($value);
                $item->save();
                break;
        }

    } else {

        header('HTTP 400 Bad Request', true, 400);
        echo "This field is required!";

    }

If someone knows better ways to do, I'm always up for new advices :)

Best,

Harri

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...