Only one button working despite formaction - php

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>

Related

how can I get the value of a input field using PHP if there are two forms on the page both of POST method

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>

Input in HTML not connecting to PHP post method

I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit

different form field required depending on submit clicked

I was wondering if it was possible to make a form field required if I submit my form with one button and not required if I click on another button.
Exemple:
<form method="post" action="action.php" name="form1">
<input type="text" name="participant_name" /> //required if submit button = save_button
//not required if submit button = cancel_button
<input type="submit" value="save" name="save_button" />
<input type="submit" value="cancel" name="cancel_button" />
</form>
Thx everyone :)
You can add the attribute: required to the input. And make from the cancel button a non submit button with u goback link with javascript.
<form method="post" action="action.php" name="form1">
<input type="text" name="participant_name" required/>
<input type="submit" value="save" name="save_button" />
<input type="button" value="cancel" onclick="window.history.back();"/>
Otherwise you should write a javascript/jquery script that validates the form after form submit.
Here is running code: https://jsfiddle.net/zmm896n2/

html - how to give a submit button a variable for GET?

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>

Performs form action even out of the form

Hi having a problem with this code even if i clicked on cancel it will still proceed to use the action of my form even though the CANCEL button is not part of the form, Would appreciate any help.
Here is the code.
<form method="post" action="input_enroll.php" >
<div id="overlay1">
<div>
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" >
<input type="submit" value="Proceed To Enroll" '>
</form>
<input type="submit" value="Cancel" onclick='overlay()'>
</div>
</div>
EDIT: Any suggestions what would be a better idea? I'm thinking putting the cancel outside the div.
You are having form started, then divs started, then form closed..start form after divs..
as your markup is not correct, browsers will change it as thier parser suggest,
In chrome </form> tag is postponed after </div>s..
<div id="overlay1">
<div>
<form method="post" action="input_enroll.php" >
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" />
<input type="submit" value="Proceed To Enroll" />
</form>
<input type="submit" value="Cancel" onclick='overlay()' />
</div>
</div>

Categories