different form field required depending on submit clicked - php

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/

Related

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

Submit auto form without submit button

I want to auto submit form after 5 minutes without submit button my code is working but not get post value.Please help me.
<form name="addcontact" method="post" content="2;" action="URL=demo.php">
<input type="text" name="addontable" value="Add on Table" />
</form>
You have a wrong value for action attribute of the form - change it to:
<form name="addcontact" method="post" content="2;" action="demo.php" content="2;">
<input type="text" name="addontable" value="Add on Table" />
</form>

2 Forms in a page, Get the Value from the other form

I have 2 HTML form in a page
<form action="my-page.php" method="post" id="savedata" name="savedata">
<input class="text" name="myname" value="" type="text" />
<input class="text" name="myaddress" value="" type="text" />
<input type="submit" value="Save" />
</form>
<form action="my-page.php" method="post" id="previewdata" name="previewdata">
<input type="submit" value="Preview" />
</form>
The first one [savedata] will save the data to MySQL after clicking the [Save Button]
The second one [previewdata] will preview (means will just show in the page using HTML) the data once the [Preview Button] was clicked
How can the 2nd form get the data from the 1st form?
As an alternative to using 2 different forms you could have a single form with 2 submit buttons:
<form action="my-page.php" method="post">
<input class="text" name="myname" value="" type="text" />
<button type="submit" name="action" value="save">Save</button>
<button type="submit" name="action" value="preview">Preview</button>
</form>
Now inside your server side script look for $_POST["action"] and act accordingly.
Also in HTML5 there's the formaction attribute that could be specified on a submit button to invoke a different server side script.

Form with two button submit with different value

<form action="here.php" method="POST">
<input type="text" name="text">
<div id="one">
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</div>
<div id="two">
<input type="hidden" name="aaa" value="two">
<input type="submit" value="Send">
</div>
</form>
Now if i click on Send of div ONE or div TWO i have always in $_POST['aaa'] = 'two';
Is possible make one form with two submit with different values?
If i click on div one submit i would like reveice $_POST['aaa'] = 'one' and if i click on div two submit i would like receive $_POST['aaa'] = 'two'.
How can i make it?
I can use for this PHP and jQuery.
EDIT:
I dont want create two form - i dont want showing two many times <input type="text" name="text">
EDIT: maybe i can instead button submit ? but how?
It seems that what you actually want to do is have a value in each of the buttons, see this, for example:
<form action="demo_form.asp" method="get">
Choose your favorite subject:
<button name="subject" type="submit" value="fav_HTML">HTML</button>
<button name="subject" type="submit" value="fav_CSS">CSS</button>
</form>
You'd need two different forms:
<div id="one">
<form ...>
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</form>
</div>
<div id="two">
<form ...>
<input ...>
<input ...>
</form>
</div>
Standard practice is that when two fields have the exact same name, to use only the LAST value encountered in the form and submit that.
PHP does have a special-case notation (name="aaa[]") to allow submitting multiple values with the same name, but that wouldn't help you here, as that'd submit ALL of the aaa values, not just the one closest to the submit button.
HTML form:
<form ...>
<input type="text" name="textfield">
<div id="one">
<input type="hidden" name="one_data" value="aaa" />
<input type="submit" name="submit_one" value="Submit" />
</div>
<div id="two">
<input type="hidden" name="two_data" value="bbb" />
<input type="submit" name="submit_two" value="Submit" />
</div>
</form>
server-side:
if (isset($_POST['submit_two'])) {
$data = $_POST['two_data'];
} else if (isset($_POST['submit_one'])) {
$data = $_POST['one_data'];
} else {
die("Invalid submission");
}
Instead of showing two submit button, you can show a radio list with two options and one submit button.
Try this:
in html-
<input id="PreviousButton" value="Previous" type="submit" />
<input id="NextButton" value="Next" type="submit" />
<input id="Button" name="btnSubmit" type="hidden" />
in jOuery-
$("#PreviousButton").click(function () {
$("#Button").val("Previous");
});
$("#NextButton").click(function () {
$("#Button").val("Next");
});
then you can see in the form results - what "Button" contains.

PHP Send query string with button

I have a form for searching products which will be visible in each page. When the search button is clicked it will redirect to a search.php page. I want to send a query string (parameter) with the content of the search text box when the button is clicked. The code for the form is simple, but here it is:
<form method="post" action="search.php">
<input type="text" id="txtSearch" name="txtSearch" class="searchInput" value="" />
<input type="submit" id="btnSubmit" name="btnSubmit" value="Search" ?>'" />
</form>
I want that when clicked this will redirect to search.php?q=txtSearch. Thanks.
Change "POST" to "GET" on the form element and change the input name parameter to "q".
use :
<form method="get" action="search.php">
<input type="text" id="txtSearch" name="q" class="searchInput" value="" />
<input type="submit" id="btnSubmit" name="btnSubmit" value="Search" />
</form>
<form method="post" action="search.php?q=txtSearch">
<input type="text" id="txtSearch" name="txtSearch" class="searchInput" value="" />
<input type="submit" id="btnSubmit" name="btnSubmit" value="Search" />
</form>
This way submitting the form you'll be redirect to search.php?q=txtSearch and from php you can get the $_POST variable so
$_POST['txtSearch']
$_POST['btnSubmit']
Just use GET instead of POST
<form method="get" action="search.php">
The name of the fields will be the name of the querystring variable. Like this:
?input_name=input_value

Categories