Asked  2 Years ago    Answers:  5   Viewed   60 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

4

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
  }
}
Friday, November 18, 2022
3

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.

Friday, September 23, 2022
1

Better way to do is..

<?php

if( isset($_POST['submit']) )
{
    $prefix     =   $_POST['prefix'];
    $firstname  =   $_POST['firstname'];
    $lastname   =   $_POST['lastname'];
    $phone      =   $_POST['phone'];
    $school     =   $_POST['school'];
    $teammate1  =   $_POST['teammate1'];
    $teammate2  =   $_POST['teammate2'];
    $games      =   $_POST['games'];
    $division   =   $_POST['division'];

    mysql_connect("localhost", "acsp", "passwordhidden");
    mysql_select_db("logicgames");
    $order = "INSERT INTO data_logicgames (prefix, firstname, lastname, phone, school, teammate1, teammate2, games, division) VALUES ('$prefix', '$firstname', '$lastname', '$phone', '$school', '$teammate1', '$teammate2', '$games', '$division')";
    $result = mysql_query($order);
    if ($result) {
        echo "<p>Success</p>";
    } else {
        echo "<p>Failed</p>";
    }
}
else
{
    echo "<p>Failed</p>";
}
?>
Thursday, August 25, 2022
 
salil
 
4

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, September 12, 2022
 
4

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.

Wednesday, December 7, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages