Asked  7 Months ago    Answers:  5   Viewed   35 times

please i need your help with an issue. I have two forms on my homepage that i'll like users to fill and submit at different times. My problem is that i'll like to have only one processing page for both of them. Normally i can do this in seperate pages. But i'll like to know if doing it on the same page is possible.

Okay.. If i submit form A, on the action page, wont there be Undefined Index for variable of form B, that has not being submitted, and ofcourse using a GET is not adviced.

Thanks for your time and patience.

 Answers

26

It's not completely unheard of to do this. Quite often, a different parameter is passed in the form element's action attribute like /submit.php?action=register or /submit.php?action=activate.

So, you have code like this:

if ($_GET['action'] == 'register') {
  // Register user
} else if($_GET['action'] == 'activate' {
  // Activate user
}

However, you could also just change the value of the submit button and have the action attribute the same for both forms:

if (isset($_POST['submit'])) {
  if ($_POST['submit'] == 'register') {
    // Register user
  } else if($_POST['submit'] == 'activate') {
    // Activate user
  }
}
Wednesday, March 31, 2021
 
Floris
answered 7 Months ago
62

It will submit the content encapsulated by the <form></form> tags. Having several form's action attribute point to the same page should not create the problem you describe.

The code you wrote here looks fine. Check your HTML code, and ensure you have the corrent <form></form> tags surrounding the elements of each form.

Wednesday, March 31, 2021
 
KingCrunch
answered 7 Months ago
41

Something like this should work

.
.
.
$q = array();
if(trim($_POST["b"]) !== ""){
    $q[] = "Program = :program";
}
if(trim($_POST["c"]) !== ""){
    $q[] = "Descr = :descr";
}
if(trim($_POST["d"]) !== ""){
    $q[] = "Cost = :cost";
}
if(sizeof($q) > 0){//check if we have any updates otherwise don't execute
    $query = "UPDATE links SET " . implode(", ", $q) . " WHERE Id= :id";
    $stmt = $dbh->prepare($query);
    $stmt->bindParam(":id", $_POST["a"]);
    if(trim($_POST["b"]) !== ""){
        $stmt->bindParam(":program", $_POST["b"]);
    }
    if(trim($_POST["c"]) !== ""){
        $stmt->bindParam(":descr", $_POST["c"]);
    }
    if(trim($_POST["d"]) !== ""){
        $stmt->bindParam(":cost", $_POST["d"]);
    }
    $stmt->execute();
}
.
.
.
Saturday, May 29, 2021
 
Shobit
answered 5 Months ago
35

Class based views are still new, so I'll write this out. The process is simple:

First, create the forms for your objects. One of the forms will be repeated. Nothing special to be done here.

class SonInline(ModelForm):
    model = Son

class FatherForm(ModelForm):
    model = Father

Then, create your formset:

FatherInlineFormSet = inlineformset_factory(Father,
    Son,
    form=SonInline,
    extra=1,
    can_delete=False,
    can_order=False
)

Now, to integrate it with your CreateView:

class CreateFatherView(CreateView):
    template_name = 'father_create.html'
    model = Father
    form_class = FatherForm # the parent object's form

    # On successful form submission
    def get_success_url(self):
        return reverse('father-created')

    # Validate forms
    def form_valid(self, form):
        ctx = self.get_context_data()
        inlines = ctx['inlines']
        if inlines.is_valid() and form.is_valid():
            self.object = form.save() # saves Father and Children
            return redirect(self.get_success_url())
        else:
            return self.render_to_response(self.get_context_data(form=form))

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))

    # We populate the context with the forms. Here I'm sending
    # the inline forms in `inlines`
    def get_context_data(self, **kwargs):
        ctx = super(CreateFatherView, self).get_context_data(**kwargs)
        if self.request.POST:
            ctx['form'] = FatherForm(self.request.POST)
            ctx['inlines'] = FatherInlineFormSet(self.request.POST)
        else:
            ctx['form'] = Father()
            ctx['inlines'] = FatherInlineFormSet()
        return ctx

Finally, here is the template:

The key part is the jquery django-dynamic-formset plugin that keeps adding new inline forms:

<form id="father-form" method="POST" enctype="multipart/form-data" action=".">
{% csrf_token %}
<div class="row">
  {% for f in form %}
    <div class="span3">{{ f.label }}<br />{{ f }}
      {% if f.errors %}
          {% for v in f.errors %}
            <br /><span style="color:red;">{{ v }}</span>
          {% endfor %}
      {% endif %}
    </div>
 {% endfor %}
</div>
<hr />
<h2>Sons:</h2>
<table class="table-striped">
 <table>
 {%  for f2 in inlines %}
   <tr id="{{ f2.prefix }}-row">
      {% for i in f2 %}
        <td>
           {{ i }}{% if i.errors %}<span style="color:red;">{{ i.errors }}</span>{% endif %}
        </td>
      {% endfor %}
   </tr>
 {% endfor %}
</table>
{{ inlines.management_form }}
<input type="submit" class="btn btn-primary" value="Go Go Gadget &rarr;">
</form>
<script type="text/javascript">
    $(function() {
        $('#father-form tr').formset({
            prefix: '{{ inlines.prefix }}'
        });
    })
</script>
Monday, August 9, 2021
 
Evernoob
answered 3 Months ago
54

Instead of

$query = mysqli_query("SELECT * FROM subscriptions WHERE email='$email'");

use

$query = $con->query("SELECT * FROM subscriptions WHERE email='$email'");

or

$query = mysqli_query($con, "SELECT * FROM subscriptions WHERE email='$email'");

Also instead of

$insert_email = mysqli_query("INSERT INTO subscriptions (email) VALUES ('$email')");

use

$insert_email = $con->query("INSERT INTO subscriptions (email) VALUES ('$email')");

These are the only 2 errors that I could see.

Friday, September 3, 2021
 
Sheakspear Zitouni
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :