SUBMIT button value sent only when clicked - php

I have the following web form:
<form action="processor.php" method="post">
<input type="checkbox" name="cb" onclick="submit()">
<input type="submit" name="search" value="Search">
</form>
So either clicking the submit button or the checkbox this form is submitted to process.php page. But submit's value named "search" is sent only when button clicked explicitly, not when checkbox is clicked. Unexpectedly for me. I expected that submitting the form with submit() command will send all parameters as well (including submit button's "search" parameter).
So in PHP code I cannot use:
if(isset($_POST['search'])
to test if form was submitted, I have to use:
if($_SERVER['REQUEST_METHOD']=='POST')
Is this normal behaviour of submitt button?

Yes this the correct behavior.
The value of a submit input is only send when activated or clicked, since here you submit the form through the function it's logical.
Check this example:
<form action="edit.php" method="post">
<input type="text" name="name">
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Preview">
</form>
This behavior allow multiple submit action in a form.
__
On checkbox click simulate a click on the submit button instead of using submit()

Yes it's normal. I have made some code for you to make it work:).
Add a class to your checkbox and submit button. And add the js code to your html or separate js file.
<form action="processor.php" method="post">
<input type="checkbox" class="checkbox" name="cb" onclick="submit()">
<input type="submit" class="sub" name="search" value="Search">
</form>
It goes in this function when you click on the checkbox, it is seeing when its enabled and if it's so simulate a click on the submit button.
$(".checkbox").change(function() {
if(this.checked) {
$(".sub").click();
}
});

Related

use only input for change image without submit

I want use that form for change image but without click to submit.
<form action="chav.php" method="POST" enctype="multipart/form-data" >
<label for="fileup"><img src="/images/img-ico.png"/>Aggiungi Foto</label>
<input type="file" name="fileup" id="fileup" /><br/>
<input type="submit" name='submit' value="Upload" />
<input type="hidden" value="<?php echo milldigital_filter('get',$_REQUEST['pg']); ?>" name="pg" />
</form>
so when i select image, directly submit without press any button.
You can submit form using javascript within form event attribute onchange:
...
<input type="file" name="fileup" id="fileup" onchange="this.form.submit()" />
...
When browser render form element with onchange property it starts to observe if its value changes and execute javascript code when it does. It changes when you select file. documentation
this.form means we'll refer to form object that this html (form) element belongs to. Then execute its submit() method.

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/

fetching value with the submit button in HTML?

If the submit button is pressed in HTML I need to send some value with it. How can I do it?
<input type="submit" name="ask" class="tbutton" value="ask" />
if(isset($_POST['ask'])){
// i need to fetch some value that is passed with submit button }
I think you want to fetch value of some input right?
may be this will help
<form method="post">
<input type="text" name="myText" />
<input type="submit" name="ask" class="tbutton" value="ask" />
</form>
<?php
if(isset($_POST['ask'])){
echo($_POST['myText']);
}
?>

How do I get the value of a radio button in PHP?

I've created a basic website that requires the user to select a radio button. I want a PHP file to retrieve the value of the radio button that was chosen and respond accordingly, but the file does not currently produce any output. What is wrong with the code I am using now? Why can my PHP file not retrieve the radio button value properly?
Index.html:
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
</form>
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
Result.php:
<?php
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
?>
Your are using two separate forms for your general input elements and one consisting of a submit button only.
Include the submit button in the first form and it should work fine:
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
<form method="post">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page-->
<input type="radio" name="MyRadio" value="Second">Second
</br>
<input type="submit" value="Result" name="Result"> <!--This button opens Result.php-->
</form >
In my php code you can see that the function of isset() that set that when your PHP code run. In your code you mention $radioVal = $_POST["MyRadio"]; where MyRadio is undefined index for PHP. Here when we submit the form then submit call the PHP code without any lag and you also use the double form. This is wrong for this code.
<?php
if (isset($_POST['Result']))
{
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
}
?>
This is the form that's being submitted. It has an action attribute which directs it to Result.php.
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
In order for you to get the data you want in Results.php, you need to add the radio buttons to this form
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br>
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result">
</form>
You're also going to need to change your method to POST if you're going to use the $_POST superglobal
$radioVal = $_POST["MyRadio"];
First of all you are doing it a little wrong. You are using two forms to do the task. Let me tell you how can you do it.
index.html
<form action= "result.php" method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page -->
<input type="radio" name="MyRadio" value="Second">Second
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
result.php
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>
You are using two separate forms for the html code, which means the first form is actually not submitted when you press the button.
You shouldn't need to change the PHP code in result.php, but you should ideally use one form.
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>

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>

Categories