Is there a way to send exactly same variables to another page different from action of the form? I tried this structure but the second submit button did not work;
<form name="2" action="page2" method="post" >
<form name="1" action="page1" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1">
</form>
<input type="submit" value="Submit2">
</form>
The form 1 shows the information of inputs on the page1 and updates database also.
I want form 2 to show the information of inputs on the page2 only (no update of database).
Is that possible?
You cannot have form inside another form , change your code like this.
<form name="2" action="page2" method="post" >
<input type="radio" name="radio" value="value1" >
<input type="radio" name="radio" value="value2" >
<input type="radio" name="radio" value="value3" >
<input type="submit" value="Submit1" name="submit1">
<input type="submit" value="Submit2" name="submit2">
</form>
Give different name for each submit button.
in php
if(isset($_POST['submit1'])){
// submit1 is pressed
}
if(isset($_POST['submit1'])){
// submit2 is pressed
}
Changing action dynamically.
add class to submit buttons say, class="submit". ANd add id to form say id="my-form"
$(".submit").change(function() {
var action = $(this).val() == "submit1" ? "submit1.php : "submit2.php";
$("#my-form").attr("action", action);
});
Related
<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>
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>
Why not alert 0 and 1 and 0 and 1 when press submit button on this function ?
When press submit, it's will alert 1,1, null , null.
Why not alert null,1,null,1 ?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<?PHP
include("connect.php");
?>
<?php
if(isset($_POST["submit"]))
{
for($i=0;$i<count($_POST["a_text"]);$i++)
{
$a_check = mysql_real_escape_string($_POST['a_check'][$i]);
?>
<script>
alert("<?PHP echo $a_check; ?>");//
</script>
<?PHP
}
}
?>
<form name="f_name" method="post" action="">
<input name="a_text[]" type="text">
<input name="a_check[]" type="checkbox" value="1">
<br>
<input name="a_text[]" type="text">
<input name="a_check[]" type="checkbox" value="1" checked >
<br>
<input name="a_text[]" type="text">
<input name="a_check[]" type="checkbox" value="1">
<br>
<input name="a_text[]" type="text">
<input name="a_check[]" type="checkbox" value="1" checked >
<br>
<input type="submit" name="submit" value="OK"/>
</form>
When you post array of checkbox then it only post Selected checkbox. So, In your case first
and third not selected and second and fourth is selected that's why it return null,1,null,1
If you are select second and fourth checkbox then it will post checkbox like as follow
a_check = [null,1,null,1];
You want to access all four checkBox through loop actually which is not posted If you are not checked.
I wanna make a checkbox that when user check and submit its appear specific elements.
this is the selection form in admin.php
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
and the selected part will be shown in user.php. each option has its own php file so I got 3 files adv.php, ann.php and pic.php. and using <?php include 'adv.php'; ?> to show in user.php
any suggestion how to do it ??
Update your form like this;
<form action="/html/codes/html_form_handler.cfm" method="get">
<input type="radio" name="page_type" value="adv" > advertisement<br>
<input type="radio" name="page_type" value="ann" > announcement<br>
<input type="radio" name="page_type" value="pic" > picture <br>
<input type="submit" value="Submit">
</form>
And at the backend;
$pages = array ("adv", "ann", "pic");
$page_type = $_GET["page_type"];
// Validate page type
if (in_array($page_type, $pages)) {
include $page_type . ".php";
} else {
die("Invalid page type");
}
well today I want to learn how to grab multiple input type button value compare to original value and last submit to php script! How can I do that with Jquery?
<html>
<body>
<div id="leftDiv" class="container">
<form action="" method="get">
<fieldset>
<p>Something 0</p>
<lable>Yes</label>
<input type="radio" name="Group0" value="1" >
<lable>No</label>
<input type="radio" name="Group0" value="0" >
</fieldset>
<fieldset>
<p>Something 1</p>
<lable>Yes</label>
<input type="radio" name="Group1" value="1" >
<lable>No</label>
<input type="radio" name="Group1" value="0" >
</fieldset>
<fieldset>
<p>Something 2</p>
<lable>Yes</label>
<input type="radio" name="Group2" value="1" >
<lable>No</label>
<input type="radio" name="Group2" value="0" >
</fieldset>
<fieldset>
<input type="submit" name="update" class="button" value="Submit">
</fieldset>
</form>
</div>
</body>
</html>
the above values are generated by php script (can be 1 or 100)
the easiest method is actually to set the correct values as selected in the HTML and tie a .onChange event to them. otherwise, have a hidden field with the original value, and onChange compare them like this
//a hidden input field with the original value
<input type='hidden' id='Group0' value='0'/>
// a set of radio fields that reference the original field we will be comparing with in their 'rel' attribute for easy jQuery selection
Off<input type='radio' name='Group0' rel='#Group0' class='compareOnChane' value=0/><br/>
On <input type='radio' name='Group0' rel='#Group0' class='compareOnChane' value=1/>
<script>
$(document).ready(function(){
$('.compareOnChange').live('change',function(){
if($($(this).attr('rel')).val()!=$(this).val(){
//the value currently is different than the original value, fire our action
}
});
});
</script>
This is a pretty good tutorial that will teach you the basics of this:
http://trevordavis.net/blog/ajax-forms-with-jquery
You can get the values of the radio buttons in a JSON like so:
var radios = {};
$('input[type="radio"]').each(function() {
radios[$(this).attr('name')] = $(this).attr('value');
});