Reputation: 9
I have an issue with a 2 different forms in one ctp file.
explanation : I want to use two forms related to different actions in same controller. I use the first form to add 2 text field a table and I use the second form just to search and retrieve the data.
MY ctp:
Form 1 adding message and Email
<?= $this->Form->create($message) ?>
<div class="form-group">
<label for="name" class="col-form-label">Name</label>
<input name="name" class="form-control" id="name" placeholder="Your Name" type="text">
</div>
<div class="form-group">
<label for="email" class="col-form-label">Email</label>
<input name="email" class="form-control" id="email" placeholder="Your Email" type="email">
</div>
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
Form 2 Searching field :
<?= $this->Form->create(null, ['url' => ['action' => 'search']]) ?>
<div class="form-group">
<label for="what" class="col-form-label">What?</label>
<input name="what" class="form-control" id="what" placeholder="What are you looking for?" type="text">
</div>
<div class="form-group">
<?php echo $this->Form->input('country_id', [
'options' => $countries,
'id' => 'country_id',
'label' => ['text' => __('Where?')]
]); ?>
</div>
<button type="submit" class="btn btn-primary width-100">Search</button>
<?= $this->Form->end() ?>
So i clicked on submit it works fine but when I clicked on search it doesn't go to the desired action it is still in the same action . Thank you!
Upvotes: 0
Views: 226
Reputation: 9
The problem was solved by replacing this code:
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
By this :
<?php
echo $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end();
?>
Upvotes: -1
Reputation: 5098
This code is not doing what you think it's doing:
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
It will echo the submit button but NOT the form end tag. You then open another form, but the browser may interpret this as a bad tag and ignore it. (Technically, I think the browser behaviour towards this malformed HTML is undefined, so you might get different behaviour from different browsers.)
Try this instead:
<?php
echo $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end();
?>
or
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end() ?>
or
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]) .
$this->Form->end() ?>
I'd recommend the first option as being much clearer code and less prone to accidental breakage with future edits; I would never allow either of the latter two on a project I was managing.
Upvotes: 3