Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kickthemooon's avatar

Save draft and publish button

I have a draft field in my db for the table offers. Now I would like to have two buttons, "save as draft" and "publish offer" for the offer form. If the user clicks the save as draft button I want the field to be 1, if the user clicks publish offer I want the field to be 0.

Any suggestions?

0 likes
11 replies
spyworld's avatar

You should reverse. "save as draft" is 0 and "publish offer" is 1. You can use jQuery for auto save.

$(#textrea).change(function(){
//ajax function
});

You don't need to separate into 2 button. Use HTML 5 select element. You can use partial view method.

2 likes
kickthemooon's avatar

unfortunately i dont understand anything, i am really very very bad with jquery or javascript :) if you could write out some code for me, id be very thankful @spyworld

thomaskim's avatar
Level 41

No need for JavaScript in this case. Just add two input buttons.

<input type="submit" name="publish" value="Publish Offer">
<input type="submit" name="save" value="Save as Draft">
if ($request->has('save'))
{
// draft
}
else if ($request->has('publish'))
{
// publish
}
4 likes
kickthemooon's avatar

@thomaskim im trying this:

$offer = Offer::find($id);
        $offer->update($request->all());

        if ($request->has('save')) {
            $offer->update([
                'draft' => 1
            ]);
        } else if ($request->has('publish')) {
            $offer->update([
                'draft' => 0
            ]);
        }

        $offer->category()->sync($request->get('offercategories', []));
        $offer->offertype()->sync($request->get('offertypes_list', []));

        return redirect('/admin/offers');

not working... my view:


            <button type="submit" name="publish" class="btn btn-primary"><i class="fa fa-save"></i> OBJAVI PONUDU</button>


            <button type="submit" name="save" class="btn btn-primary"><i class="fa fa-pencil-square-o"></i> SNIMI PONUDU</button>

thomaskim's avatar

Those are button tags. You need to be using the input tag with the submit type. :) The input's "value" is also the text of the button.

1 like
spyworld's avatar
<select name="publish">
<option value="0">Save as Draft</option>
<option value="1">Publish Offer</option>
</select>
$draft = 0;

if($request->get('publish')){
    $draft = 1;
}

$offer->update([
    'draft' => $draft
]);
4 likes
kickthemooon's avatar

hmm whats wrong with this? @thomaskim

<input type="button" name="publish" class="btn btn-primary"><i class="fa fa-save"></i> OBJAVI PONUDU </input>
RomainLanz's avatar

Your input need to have a type of submit.

<input type="submit" name="publish" class="btn btn-primary" value="OBJAVI PONUDU ">
1 like
LostKobrakai's avatar

@kickthemooon You're missing the value attribute on the button. It's mostly irrelevant if you're using a button or input[type=submit], but both won't submit any data if they don't have a name and value.

1 like

Please or to participate in this conversation.