i have the following button:
<form method="GET" action="/cms/deleteBlog/">
<input class="btn btn-danger btn-small" type="submit" name="'.$blogID.'" value="Delete">
</form>
So right now i get the following url:
cms/deleteBlog/?1=Delete
obviously i want something like the following:
cms/deleteBlog/id=1
So i tried the following (which is obviously not working):
name="id='.$blogID.'"
So how do i solve this? i've been looking around on the internet, and i know the solution is quite easy. But i just can't remember the way how it is done!
Add a hidden form field.
<input type="hidden" name="id" value="1" />
Also, you should never use GET to delete or modify data. Read Why should you delete using an HTTP POST or DELETE, rather than GET? for insight.
Why not use a hidden input field, e.g:
<form method="GET" action="/cms/deleteBlog/">
<input type="hidden" name="id" value="'.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>
Or, if you want pretty URLs:
<form method="GET" action="/cms/deleteBlog/id='.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>
Related
I want to get the value of an <input> field using PHP.
There are two forms on my page both of POST method.
<form method="POST">
<input type="text" name="first">
<input type="submit" name="submit" value="submit">
</form>
<form method="POST">
<input type="text" name="second">
<input type="submit" name="submit1" value="Post">
</form>
How do I get the value of the second input field? Even though if I use $_POST['second'] it shows me an error:
Undefined index: 'second'
The W3C specs define that an input can be associated to only one form. It is a sign of bad design when you need to have multiple forms and the backend has to know which data is present in other forms.
The <form> element can contain arbitrary element structures like tables, it can even contain the entire document body content. You should hardly need multiple forms.
A common use-case is to have multiple submit buttons having the same name instead. Only the pressed button will be part of the form data.
<form method="post">
<input type="text" name="text_input">
<button type="submit" name="action" value="add">submit</button>
<button type="submit" name="action" value="update">submit</button>
<button type="submit" name="action" value="delete">submit</button>
</form>
Again, do not do that, however, if you really want to share a field across multiple forms for some reason, this can only be done by javascript intercepting the form submit event. This will not work when scripts are disabled by the user.
document.querySelectorAll('form').forEach(e => {
e.addEventListener('submit', function() {
document.querySelectorAll('.multi-form-input').forEach(e => e.setAttribute('form', this.id));
})
})
<input class="multi-form-input" name="common_input" type="text">
<form id="form-1" method="post">
<button type="submit" name="action" value="1">submit</button>
</form>
<form id="form-2" method="post">
<button type="submit" name="action" value="2">submit</button>
</form>
I have a file.php that is echoing the contents of a html file. The solution to my problem seems to be to use formaction according to stackoverflow but for some reason I can't get it to work. 'Createuser' works but not 'shop'. I have tried with not having formaction on the 'shop' button as well since I already have action = shop but it doesn't work. What am I doing wrong?
<form method="post" action="shop.php">
<table>
<p>
<input type="submit" value="Sign in" name="sign_in" form method="post" formaction="shop.php"/>
<input type="submit" value="Create user" name="create_user" formaction="createuser.php" />
</table>
</p>
</form>
The first button shouldn't need formaction as it is defined in the form properties.
Could you try changing your inputs to buttons? something like.
<button type="submit" formaction="createuser.php">Create User</button>
It is important to use type="submit" here or formaction will be ignored.
Try removing the form action, form and method attributes from the sign in submit input. I know you said you removed formaction, but I think the below should work.
<input type="submit" value="Sign in" name="sign_in" />
I don't know if this will solve your problem but where you have your closing table tag isn't where it should be this:
<form method="post" action="shop.php">
<table>
<p>
<input type="button" value="Sign in" name="sign_in" form method="post" formaction="shop.php"/>
<input type="button" value="Create user" name="create_user" formaction="createuser.php" />
</p>
</table>
</form>
View app
I made a multi update row with js ... the button function uses put to retrieve the id of each row ...
this success but..
I want to add input form for data that is updated
I don't know what to do
<form action="{{ url('UpdateAll') }}" >
<input type="text" name="name" placeholder="test">
<input type="submit" class="btn btn-primary delete_all" value="Mass Update">
</form>
code detail
I just had a look at your code in the link provided. Is it because your ajax request PUT url is set to :
url: $(this).data('url'),
But you're not actually setting that URL on your input? eg.
This:
<input type="submit" class="btn btn-primary delete_all" value="Mass Update">
Should be:
<input type="submit" class="btn btn-primary delete_all" value="Mass Update" data-url="/your-put-url-here">
My PHP form uses multiple submit buttons that I use for A/B processing
Occasionally, the wrong submit buttons get passed on even though they are not the ones clicked. The buttons look like this:
<form method="post" action="url..." autocomplete="off">
... fields...
<input type="submit" name="UpdateExit" value="Exit" />
<input type="submit" name="UpdateSave" value="Save" />
<input type="submit" name="InsertNew" value="Insert new" />
<input type="submit" name="Delete" value="Delete" />
<input type="button" name="Cancel" value="Cancel" />
</form>
Sometimes the var_dump($_POST) shows both UpdateExit and UpdateSave even though i clicked InsertNew! Needless to say it messes up the output. Then "the right button" is sent.
I narrowed down this behavior to webkit browsers and the first time I use the form after clearing my browser's cache, then it seems to "come back to normal". This is empirical and I cannot say it's PHP or HTML related. but I have been struggling with this for the better part of the day and found no spot on info on SO or elsewhere :(
Anyone heard/encountered a similar behavior?
Try:
<form method="post" action="url..." autocomplete="off">
... fields...
<input type="submit" name="UpdateExit" value="Exit" />
<input type="submit" name="UpdateSave" value="Save" />
<input type="submit" name="InsertNew" value="Insert new" />
<input type="submit" name="Delete" value="Delete" />
<input type="submit" name="Cancel" value="Cancel" />
</form>
Handle Cancel logic as you would any of the other submit buttons.
PHP Form Handler Logic:
<?php
if (isset($_POST['UpdateExit'])) {
//Do something based on Update and Exit was used
}
elseif (isset($_POST['UpdateSave'])) {
//Do something based on Update and Save was used
}
elseif (isset($_POST['InsertNew'])) {
//Do something based on Insert New was used
}
elseif (isset($_POST['Delete'])) {
//Do something based on Delete was used
}
else {
//Do something because Cancel was used...
}
?>
I have never really used radio buttons before. So what I am trying to achieve is, using two radio buttons 'Public' and 'Private'. This is for the user's profile. My HTML code for the buttons is;
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="0">Private</button>
<button type="button" class="btn btn-primary" id="privacy" name="privacy" value="1">Public</button>
</div>
As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!
Use a form.
<form method="get" action="yourscript.php">
<input type="radio" id="private" name="privacy" value="0">
<label for="private">Private</label>
<input type="radio" id="public" name="privacy" value="1">
<label for="public">Public</label>
<input type="submit" value="Save">
</form>
Change your buttons to input elements, which, together with the form, provide a HTML native way to send data to a script without the need for Javascript (although you can use Javascript to enhance the usability later on).
Using the label tag, you can assign a text to a radiobutton. This allows this text to be clicked as well, and it also provides better support for screen readers, because they can actually see which text belongs to the radiobutton.
An id needs to be unique, so I changed it to private and public. The ids are also used to link the label to.
The name determines by which name the value is sent to your script.
In PHP, you can use the superglobal $_GET. $_GET['privacy'] will contain '0' or '1' depending on the choice made. If the method of the form was post, you could use the superglobal $_POST instead, or you can use $_REQUEST, which contains values from either, so in that case your script doesn't care whether the values were send in the url (get) or in the chunk of invisible post data that is sent along with the request.
change type to radio and donot use the same id for morethan one element id is supposed to be unique
Also when you post the form type=button values will not be passed with the form
<div class="btn-group" data-toggle="buttons-radio">
<input type="radio" class="btn btn-primary" name="privacy" value="0"/>Private
<input type="radio" class="btn btn-primary" name="privacy" value="1"/>Public
</div>
assuming its posted on $_POST['privacy'] should contain either a 1 or a 0
also dont use an ID twice - they are supposed to be unique
You don't have form and submit button.
HTML page:
<div class="btn-group" data-toggle="buttons-radio ">
<form name="privacyForm" action="action.php" method="post">
<button type="radio" class="btn btn-primary" name="privacy" value="0">Private</button>
<button type="radio" class="btn btn-primary" name="privacy" value="1">Public</button>
<input type="submit" value="Submit">
</form>
</div>
action.php
<?php
echo $_POST['privacy']; // should be 0 or 1
?>
i have assumes that your form is something as follows.
<form name="sample" action="form_submit.php" method="POST">
<div class="btn-group" data-toggle="buttons-radio">
<input type="button" class="btn btn-primary" id="private" name="privacy" value="0">Private</input>
<label for="private">Private</label>
<input type="button" class="btn btn-primary" id="public" name="privacy" value="1">Public</input>
<label for="public">Public</label>
</div>
<input type="submit" name ="submit" value ="Submit"/>
</form>
you can use the following code inside the action (form_submit.php) to get the selected radio button value.
form_submit.php
<?php
$selected_radio = $_POST['privacy'];
print $selected_radio;
?>
make sure that you are not duplicating the IDs because they should be unique.