Multiple submit buttons on CakePHP form causes black-hole - php

I have a form in my CakePHP with two submit buttons like so:
<div class="form-actions">
<button type="submit" name="preview" class="btn btn-large">Preview</button>
<button type="submit" name="save" class="btn btn-large">Save</button>
</div>
However because I've given them both names it returns with an error that the request has been black-holed due to the security restrictions.
How do I get around this? As I want to do different things in the controller dependant on which button was used to submit the form.

Does it work if you give them both the same name but different values:
<input type="submit" name="submitButton" value="Preview" />
<input type="submit" name="submitButton" value="Save" />

Can you do this instead?
<div class="form-actions">
<button type="button" name="preview" class="btn btn-large">Preview</button>
<button type="submit" name="save" class="btn btn-large">Save</button>
</div>
$(function() {
$(".btn.btn-large").on("click", function(){
$( "#yourForm" ).submit();
});
});

Doing this works:
<?php
echo $this->Form->submit('Save and Preview', array('div'=>false, 'name'=>'submit', 'value'=>'preview'));
echo $this->Form->submit('Publish Post', array('div'=>false, 'name'=>'submit', 'value'=>'publish'));
?>

Related

My iconpicker button submits my HTML form

I have a problem with my iconpicker: In my form, i want the user to choose an icon. So i added an iconpicker button from FontAwsome. If i put it out of the form, it works. But when i put it into the form, what i just need to do, when i click on it, it submits the form.
I've tried to write "type='button'" but it not works.
Can someone help me and show what's wrong?
Thank you!
<body>
<form method="post" id="form1" action="page_generator.php" enctype="multipart/form-data">
<div id="free_links">
<p >other</p>
<div class="form row" id="free_a_cloner"style="display:none" >
<div class="form-group col-1">
<button name="flnk_0[]" id="ipk" class="btn btn-outline-secondary" data-icon="fas fa-mouse-pointer" role="iconpicker" ></button>
</div>
</div>
</form>
<div class="form row">
<div class="col-sm-10">
<button type="submit" form="form1" class="btn btn-primary">Submit</button>
</div>
</div>
<script>
$("#ipk").iconpicker();
</script>
</body>
Specify that the buttun isn't of type submit by adding type='button':
<button type='button' name="flnk_0[]" id="ipk" class="btn btn-outline-secondary" data-icon="fas fa-mouse-pointer" role="iconpicker" ></button>
you forget to close a div and also try with type="button". sometime it may cause issues. Try the code below and better to create a pen for better understanding or provide head code as well thanks.
<body>
<form method="post" id="form1" action="page_generator.php" enctype="multipart/form-data">
<div id="free_links">
<p >other</p>
<div class="form row" id="free_a_cloner"style="display:none" >
<div class="form-group col-1">
<button type="button" name="flnk_0[]" id="ipk" class="btn btn-outline-secondary" data-icon="fas fa-mouse-pointer" role="iconpicker" ></button>
</div>
</div>
</div>
</form>
<div class="form row">
<div class="col-sm-10">
<button type="submit" form="form1" class="btn btn-primary">Submit</button>
</div>
</div>
<script>
$("#ipk").iconpicker();
</script>
</body>
Add type="button" and check source code on browser because it could be changed by javascript after page rendered and check if there is $("button").onClick.submit like javascript code.
be sure there is only 1 type property i mean not like that:
<button type="button" type="submit">
you can use this code!!
<button type="button" form="form1" class="btn btn-primary">Submit</button>

$request->has() doesn't work with Button type and using jQuery in Laravel controller

I have question on Laravel with jQuery.
Right now, I have form with three (3) different buttons (update,delete,email). Each button have different task in controller.
Normally I use button type="submit" to submit data from blade into controller.
Blade
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="submit" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="submit" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="submit" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
And in controller, I just use '$request->has()' to differentiate three request.
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
But my current goal is to show loading icon before data successfully submitted.
The reason why I want to show loading page is because system need to send an email to multiple user and it might take some time to load before it finished the task. To prevent user to click the same button multiple times.
I use jQuery click function and I change button type from 'submit' to 'button'.
But the problem is when the form submitted to controller. It doesn't recognize '$request->has()' in my controller.
Here's my html form and jQuery and my controller
blade.php
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="button" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="button" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="button" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
jQuery
$(document).ready(function(){
$('#update').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#delete').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#email').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
});
Controller.php
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
How to solve this problem ? Thanks in advance.

hidden field how to remove last part of url?

I want to remove GET parameters from urls:
http://localhost/fadt/admin/edit_country.php?id=7
I want my URLs to become:
http://localhost/fadt/admin/edit_country
here is how links are currently generated:
<button onClick="location.href='edit_country?id=<?php echo $row['COUNTRY_ID']; ?>'" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
Instead of a button use an HTML form with the POST method.
Something like:
<form action="edit_country.php" method="post">
<input type="hidden" value="<?php echo $row['COUNTRY_ID'];?>" name="id" />
<input type="submit" class="btn btn-primary btn-xs fa fa-pencil" title="Edit"/>
</form>
in place of
<button onClick="location.href='edit_country?id=<?php echo $row['COUNTRY_ID']; ?>'" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
Then on edit_country.php use $_POST instead of $_GET.
This id can be manipulated just as easily as your button value so make sure you are authenticating executing user has permissions to perform action.
Update, using button:
<form method="post">
<input type="hidden" value="<?php echo $row['COUNTRY_ID'];?>" name="id" />
<button type="submit" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
</form>
Not clear the question but i think u need this
<?php
$a = explode('?','http://localhost/fadt/admin/edit_country.php?id=7');
echo rtrim($a[0], '.php')
?>

I have a post form that submits but "$_POST" variable is empty even though my web inspector indicates otherwise

Here is my html:
<form id="quant" method="POST" action="/portfolio" name="trans">
<div class="modal-body">
Shares : <input autocomplete="off" type="text" name="quantity">
<input type="hidden" value="<?= htmlspecialchars($_GET['q']); ?>" name="stock">
<input type="submit" name="val" value="Confirm Transaction" class="btn btn-primary">
<div id="slider-range-max"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
Here is an image of my web inspector showing that in fact that data was requested:
I have been trying to figure out why this has not been working for a long time, any suggestions would be appreciated.
The Code:
if(isset($_POST["quantity"])) {
var_dump($_POST);
which returns nothing
Then you need a slash after it to let it know it is a folder and not a file: action="/portfolio/"
If you're using something like AngularJS to submit your form, it doesn't send POST data the typical way. Check out this post.

Submitting to database from modal view

I have a file, let`s name it dashboard.php, in this file i load a modal window and trough this window i want to submit a form and the form action is also written in this dashboard.php file. is this possible? i am acutally trying it this way, but it s not working:
dashboard:php
<?php $modal_gerade_geschlossen = $_POST['abgespeichert'];
if ($modal_gerade_geschlossen == 1){
$neue_addy = $_POST['neue_addy'];
$benach_ja_nein = $_POST['benach_ja_nein'];
mysql_query("INSERT INTO mitglieder (email,benachrichtigung)VALUES('$neue_addy','$benach_ja_nein')");
}
?>
<div class="modal-body" style="height:194px">
<div>
<form name="losgehts" method="post" action="../dashboard.php">
<div style="float:left">
<input name="neue_addy" style="width:270px" type="text"/>
</div>
</div>
<div>
<select name="benach_ja_nein" >
<option>ja</option>
<option>nein</option>
</select>
</div>
<input type="hidden" name="abgespeichert" value="1"/>
<button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
<input type="submit" name="submit" class="btn btn-primary" value="Speichern"/>
</form>
</div>
</div>

Categories