Send other input value - php

I'm now looking for Event_Handler & Dispatcher class, and there was that moment, to make individual class for each event.
For example I have database with some record, and i want to choose between two actions Edit & View records.
So I need to create two files class.Handler_Edit & class.Handler_View, and then depending on pressed input
<input type="submit" name="action" value="Edit"/>
OR
<input type="submit" name="action" value="View"/>
I need to get value from $_POST['action'] and call, for example, the correct class
'class.Handler_' . $_POST['action'] . '.php'
and then start for example
class.Handler_View.php
(depending on selected input).
All cool, works! But the problem is, that i'm using russian words for input value. Not value="View" & value="Edit", but value="Посмотреть" & value="Редактировать".
And then i can't call class
class.Handler_Редактировать.php
I found a solution, that i can use buttons instead inputs, for example:
<button type="submit" name="action" value="edit">Редактировать</button>`.
But is it the correct way to solve that problem?
Maybe it's not the best decision to renounce the use of inputs and use only buttons?

The best way to solve this problem would be to use if statements! Here is a code sample:
if ($_POST['action'] == "Посмотреть") {
// do something with 'class.Handler_View.php'
} else if ($_POST['action'] == "Редактировать") {
// do something with 'class.Handler_Edit.php'
} else {
//uh oh, you didnt get View or Edit!
}
This is also much safer, as the end user can change the value of the form extremely easily! Also with this method, you will be able to use either the buttons or the inputs!

Related

Best way to check which form has been submitted PHP

So I've been thinking about this for an hour and am interested in what the best way is to check which form has been submitted.
So lets say we've got formOne and formTwo that both submit to formsubmission.php and inside there I have deemed the most appropriate way to check for SUBMISSION is
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//do stuff
}
But what is the best way to actually determine WHICH form has been submitted, formOne or Two?
I have read here What is the best way to identify which form has been submitted? and am wondering whether these are the only two ways to get around this.
I figured that just checking isset($_POST[formname]) would be bad due to a few reasons I read about elsewhere, but now I am starting to think that the whole idea of posting to the same .php file is just bad practice and there is no GOOD way to check which form has been submitted doing it this way.
Is using GET and submitting each different form to a seperate ID bad practice? if so - why?
So my question is, what is the best way to check WHICH form has been submitted, that is GOOD practice?
Thanks
PS also looked at hidden fields -> doesn't seem html worthy in terms of quality
The best would be to use a different name for each from submit input :
For formOne :
<input type="submit" name="formOne" value="formOneValue" />
For formTwo :
<input type="submit" name="formTwo" value="formTwoValue" />
Then in you're php file :
if (!empty($_POST['formOne'])) {
//do something here;
}
if (!empty($_POST['formTwo'])) {
//do something here;
}
I would suggest you'd check the fields that you received if they don't have the exact same fields or have a hidden field which tells your php code which form it is, there is nothing wrong about that. However, the best option is to have separate urls for separate actions, regardless of which php file actually handles the submission in the end.
To do this you should look into urlrewrite and htaccess, which will allow you to turn a url like users/delete and users/add to myphpfile.php?action='delete' (meaning POST data is preserved) and in your php code look at the $_GET['action']value to decide which code to run.
In my CMS I have more then three from that submit onto the same page.You just need to see which form has been submitted using your submit button name and value.Your request can either be a POST or a GET. Here's one example for two form that submit onto the same page.
The form submit button name are different.
<input type="submit" name="video" id="submit" value="Save"> -- first form
<input type="submit" name="album" id="submit" value="Save"> -- second form
if(isset($_POST['video']) && $_POST['video']=='Save'){
#code for first form
}
if(isset($_POST['album']) && $_POST['album']=='Save'){
#code for second form
}

Link redirect to delete a query with PHP

Usually, when I need to delete a query, I don't know how to do it, I don't mean, I don't know the SQL code to do it. I mean I don't know the exactly way to "call" the function. The way I've found is to call the function via GET doing this:
<?php
$action = $_GET['action'];
$id = $_GET['id'];
if($action == 'delete'){
echo "This would to the query to delete the element";
}
?>
So, The way I have to call it is by GET. I create a link with the next URI:
http://localhost/test/index.php?action=delete&id=1
This actually works. But I don't like how does it works. I don't like this way, and I am wondering if there's any other way to do exactly the same, with Javascript, JSON or even AJAX.
You tried to request the delete action by POST?
Send the id on a hidden text field like this:
<form method="POST" action="linkhere">
<input type="hidden" name="id" value="<?=$idhere?>">
<input type="hidden" name="action" value="actionnamehere">
<input type="submit" value="Delete">
</form>
You can add some javascript with the confirm button too.
Using it u won't need to use the GET method and show on the URL these parameters.

One form two submit buttons

I have two similar forms on a site that I'd like to merge into one form with two submit buttons. They use most of the same data.
The first form uses GET to send the data to another server. The second form sends it in an email. I'd like to strongly encourage site users to use option one before trying option two.
I know how to do this with javascript, but not in a way that degrades well. Any other ways to have two submit options? Or other ideas for how to accomplish this? Thanks!
Snow Blind provided the good solution, but you can't determine which button was clicked.
Buttons must have different names, not the same.
Example:
<input type="submit" name="server" value="Server" />
<input type="submit" name="email" value="Email" />
<?php
if(isset($_GET['server']))
{
// Send to another server
}
else if(isset($_GET['email']))
{
// Send to email
}
else die("None of buttons was clicked.");
?>
Additionally, if you have a same code in both parts (server and email), you can do the following:
if(isset($_GET['server']) || isset($_GET['email']))
{
// Do something common to both methods
if(isset($_GET['server']))
{
// Send to server
}
else
{
// Send to email
}
}
Better solution, in my opinion, is to put only 1 submit button + a dropdown menu with method to choose.
<select name="sendMethod">
<option value="" disabled>Choose sending method...</option>
<option value="server">Send to another server</option>
<option value="email">Send to e-mail</option>
</select>
Like everybody said use either checkbox/radio button.
And set the one that use GET as the default option
If you don't want to use javascript you can always get the value and of the checkbox/radio and check user choice
You can create two submit buttons and give them the same name but different value.
<input type="submit" name="submit" value="Server">
<input type="submit" name="submit" value="Email">
Then you can check $_GET['submit'] or $_POST['submit'] (depending on form method) to see which submit was used.

$_POST not finding a "name" value

I am trying to submit a form, it SHOULD send forth a name, but I know I'm messing something silly up and I just can't see it (3 hours of sleep last night + new coding project at work != a smart idea)
Here's my form on one page:
<form action="add.php" method="POST">
<button type="submit" name="exportcult">Export All</button>
</form>
And here's the code on the other page, meant to process the POST:
if (!isset($_POST["name"]) || $_POST["name"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
I am getting the error message this sends back, so I know it isn't registering a name - why could that be?
I think you're confused how the form data actually gets submitted. "name" is the attribute, not the key value that is found in the POST data. You need to specify the name for that element, which will be the key value present in the POST data. You have specified name="exportcult" so in the POST data, the variable will be at $_POST['exportcult']. However, this value will always be an empty string since you have not indicated a value attribute for your button.
Keep in mind that when dealing with submit buttons, only the value of the button which was used to submit the form will be included along with the rest of the form data. Try using this:
<button type="submit" name="exportcult" value="export">Export All</button>
If that specific button was used to submit the form, then $_POST['exportcult'] should be equal to 'export'.
For those of you who are unsure: buttons do get submitted with the form, but they still have to have a value attribute.
Your form doesn't contain any field except the button, so $_POST will only contain a field exportcult.
Edit: Since you use <button> instead of <input> it might not go into the POSTed data.
Do:
if (!isset($_POST["exportcult"]) || $_POST["exportcult"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
You're currently checking for a field named "name", when the field is named "exportcult". Additionally, it should be <input, not <button.
you should add
<input type="button" name="exportcult" value="Whatever you want" />
and check for exportcult on the isset() instead of name
use exactly this:
<form action="add.php" method="POST">
<input type="submit" name="name" value="Export All"></form>
if (!$_POST["name"])) {
header('Location: '.$criteria."?error=data");
exit();
}

Multiple submit button in a form

Hey I am very new to Web Programming. I have been learning PHP from the past few days and I am stuck at one thing.
I have a form tag in my code which has two submit buttons to manipulate on the data.
Since I can have only one action definition on my form tag, it can lead me to one page only. (Not very sure)
Now depending on the button clicked on the form, I want to load a different page.
One way is to check the button clicked in an if-else construct and then use echo '...' in the branches and show as if it is a different page. But it doesn't seem right for some reason. Can some one give me a better solution? Thanks.
One way is to use Javascript to switch the form's action depending on which control has been clicked. The following example uses the jQuery library:
<form id="theForm" action="foo.php">
...
<input id="first" type="submit"/>
<input id="second" type="submit"/>
</form>​
$(document).ready(function() {
$("#theForm input").click(function(e) {
e.preventDefault();
if(e.target.id == 'first') {
$("#theForm").attr("action", "somePage.php");
} else {
$("#theForm").attr("action", "anotherPage.php");
}
alert($("#theForm").attr("action"));
$("#theForm").submit();
});
​});​
Demo here: http://jsfiddle.net/CMEqC/2/
But it doesn't seem right for some reason.
That's wrong assumption.
Any other solution would be much worst.
Checking on the server side is the only reliable solution.
However echo in branches isn't necessary. There are a lot other ways.
To use include statement is most obvious one.
just as a reference... int HTML5 buttons can redefine the form's action,method,type etc. http://w3schools.com/html5/tag_button.asp for me, that's a good way to control a form :)
to add another solution based on #karim79's, since it's tagged with PHP:
<form id="theForm" action="foo.php">
...
<input id="first" name="button" value="first" type="submit"/>
<input id="second" name="button" value="second" type="submit"/>
</form>​
in your foo.php, do something like this:
<?php
$submit = isset($_GET['button']) ? trim($_GET['button']) : '';
if($submit == 'first')
{
header('Location: somePage.php');
}
else if($submit == 'second')
{
header('Location: anotherPage.php');
}
?>
Summary:
to be able to read on your button (2 submit buttons), you need to add name on each one. To make it simple, just use the same name on both. Then, add different value. Next, you need to know what button is being clicked by checking what value is sent on that particular button.

Categories