0

I have a submit button in my .php file, but I want to create another that Submits and allows to fill the form again. For example: I am adding a new product and I want to add a lot of them without closing the form.

So, for the submit button I have this code:

<div id="novoServico<?= $sub->getIdSubcategoria()?>" class="modalDialog">
 <div>
    <a href="#secao_servicos" title="Close" class="close">X</a>
    <?php $data['idSubcategoria'] = $sub->getIdSubcategoria();
    $data['idEmpresa'] = $empresa->getIdEmpresa();
    $this->load->view("servico_add_view",$data); ?>
  </div>
</div>

That calls the file with this code

$data = array(
  'type'        => 'submit',
  'id'          => 'submit',
  'class' => 'button',
  'value'       => 'Inserir'
);

echo form_label('<span>&nbsp;</span>','submit');
echo form_input($data);

Can anyone tell me what function or whatever to start create the 'Submit and add again' button?

Thanks!

  • 1
    Why don't you use AJAX for that? You could submit your form without even leaving the current page. Then you can submit again the same form as many times as you want. – Arkoudinos Aug 01 '15 at 14:44
  • Maiara, take a look here: http://stackoverflow.com/questions/14235596/ajax-submitting-a-form-without-refreshing-the-page – Lajos Arpad Aug 01 '15 at 14:57

1 Answers1

0

I think that the best way to implement this is by using AJAX. Especially, I prefer jQuery+AJAX because it simplifies a lot the procedure and you're writing a lot less code.

So, in order to clarify this a bit, you have firstly to use the jQuery library. If you want to find out how there is Google, it'll be simple. After this, a typical contact form may be POSTed via AJAX in a pretty simple way.

HTML

<form method="POST" action="contact.php" id="contact_form">
    <br><table>
        <tbody><tr><td><label>Name:</label></td><td><label>Surname:</label></td></tr>
        <tr>
            <td><input type="text" id="name" name="name"></td>
            <td><input type="text" id="surname" name="surname"></td>
        </tr>

        <tr><td colspan="2"><label>Your message:</label></td></tr>
        <tr><td colspan="2"><textarea id="msg" name="msg"></textarea></td></tr>
        <tr><td><input type="button" id="sendbtn" onclick="sendform();" value="Send" ></td></tr>
    </tbody></table>
</form>

Javascript

name = document.getElementById("name").value;
surname = document.getElementById("surname").value;
msg = document.getElementById("msg").value;
$.ajax({
            type: "POST",
            url: "contact.php",
            data: {name: name,
                surname: surname,
                msg: msg},
            datatype: "html",
        }).done(function(html) {
            $(".content").html(html);
        });

Remember to wrap your js code in a function, so you can call it with the onclick event.

Arkoudinos
  • 1,099
  • 12
  • 20