I have survey page containing multiple question and radio button for each question
<table class="table">
<form action="What should i write here" method="post">
<?php $numbering = 1;
foreach ($dataQuestion as $key => $value) : ?>
<tr>
<td width="1%"><?= $numbering++; ?></td>
<td><?= $value->question ?>
<div class="form-check mt-1">
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="1">
Very Good
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="2">
Good
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="3">
Decent
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="4">
Bad
<br>
<input class="form-check-input" type="radio" name="indikator[$value->id]" value="5">
Very Bad
</div>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="2"><button type="submit" class="btn btn-info btn-lg btn-block">Submit</button></td>
</tr>
</form>
I'm trying to pass selected radio button value to my controller file using <form action=""> but every question have different answer, how to send array of all answered question to my controller via <form action=""> ?
It seems everyone prefer to use javascript, url, and form_radio(). Any help would be appreciated, thank you.
<form action="What should i write here" ...>
Write there the route/path meant to receive your HTTP POST request. I.e:
<form action="/survey" method="post">
The state a route definition meant to receive the route in app\Config\Routes.php. I.e:
// ...
$routes->post("/survey", "SurveyController::create");
Related
I am writing a simple web page to demonstrate how to use forms in PHP. The page works fine when run on the university's web server; but, when I try to run it locally through IntelliJ 16, $_POST is always empty (but php://input does contain the correct post string). The other aspects of the demo work fine.
I've read many other posts about $_POST being empty; and, from what I can tell, those situations solutions don't apply here.
I'm assuming that there is a configuration problem with my local copy of php; but, I don't know what could be mis-configured that would cause $_GET to work, but not $_POST.
This is the relevant html form:
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
The complete (working) example is here: http://www.cis.gvsu.edu/~kurmasz/StackExchange/formDemo.php
Adding <?php error_reporting(E_ALL); ?> produces no errors or
warnings.
$_SERVER["HTTP_CONTENT_TYPE"] = "application/x-www-form-urlencoded"
What should I check next?
Complete PHP source:
<!-- This file demonstrates
(1) How to set up a simple HTML form
(2) How to use PHP to access the data
-->
<?php error_reporting(E_ALL); ?>
<html>
<head>
<title>Form Demo</title>
<style type="text/css">
#get, #post {
vertical-align: top;
}
</style>
</head>
<body>
<h1>Form Demo</h1>
<h2>Questions:</h2>
<ul>
<li>Can you get data in both <code>$_GET</code> and <code>$_POST</code> at the same time? If so, how. If not, why
not?
</li>
<li>What happens if you leave a text box empty?</li>
<li>Can you "unselect" all the radio buttons? If so, how. If not, why not?</li>
<li>Can you select "Fred" and "Barney" at the same time? Can you select "Barney" and "Trumpet" at the same time?
What's the difference?
</li>
<li>What happens if you unselect all the check boxes?</li>
<li>How does PHP treat checkboxes differently when doing a POST than when doing a GET?</li>
<li>How can you tell which submit button was pushed?</li>
</ul>
<table>
<tr>
<td id="get">
<fieldset>
<legend>GET form</legend>
<form action="formDemo.php" method="get">
"text" type input named <code>theTopOne</code>
<input type="text" name="theTopOne" value="Value for Top input"/><br/>
"text" type input named <code>theSecondOne</code>
<input type="text" name="theSecondOne" value="Value for Input #2"/><br/>
Radio buttons named "flintstones":
Fred <input type="radio" name="flintstones" value="iChooseFred" checked="checked"/>
Barney <input type="radio" name="flintstones" value="iChooseBarney">
Wilma <input type="radio" name="flintstones" value="iChooseWilma"></br>
Radio buttons named "instruments":
Saxophone <input type="radio" name="instrument" value="sax"/>
Trumpet <input type="radio" name="instrument" value="tpt">
Piano <input type="radio" name="instrument" value="piano" checked="checked"></br>
Checkboxes named "courses":
451 <input type="checkbox" name="course451" value="451"/>
452 <input type="checkbox" name="course452" value="452"/>
457 <input type="checkbox" name="course457" value="457"/><br/>
<input type="submit" name="getSubmit1" value="Submit Button 1"/>
<input type="submit" name="getSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_GET</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_GET as $key => $value) {
echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
?>
</table>
</td>
<td id="post">
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
"text" type input named <code>theSecondPost</code>
<input type="text" name="theSecondPost" value="Value for Post #2"/><br/>
Radio buttons named "interest":
Cool <input type="radio" name="interest" value="itsCool" checked="checked"/>
OK <input type="radio" name="interest" value="itsOK">
Boring <input type="radio" name="interest" value="itsBoring"></br>
Radio buttons named "bestState":
Georgia <input type="radio" name="bestState" value="GA"/>
Michigan <input type="radio" name="bestState" value="MI">
California <input type="radio" name="bestState" value="CA" checked="checked"></br>
Checkboxes named "IScourses":
260 <input type="checkbox" name="IScourses[]" value="cis260" checked="checked"/>
333 <input type="checkbox" name="IScourses[]" value="cis333"/>
463 <input type="checkbox" name="IScourses[]" value="cis463"/><br/>
<input type="submit" name="postSubmit1" value="Submit Button 1"/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_POST</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_POST as $key => $value) {
$printMe = $value;
if (is_array($value)) {
$printMe = "[" . implode($value, ", ") . "]";
}
echo "<tr><td>$key</td><td>$printMe</td></tr>\n";
}
?>
</table>
</td>
</table>
<?php
$fp = fopen("php://input", 'r+');
echo stream_get_contents($fp);
?>
<hr>
<?php var_dump($_SERVER); ?>
</body>
</html>
lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)
My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values
You're setting up some attribute values for an object, such as colour, size, weight etc in a form. If using buttons to specify these values before wishing to post all the information to a php page for further processing - how do you get the values passed to php, if the buttons are not in themselves submitting the form?
For example:
<form action="processgivenvalues.php" method="post">
choose colour: <button type="button" name="colour" value="green"></button>
<button type="button" name="colour" value="blue"></button>
choose size: <button type="button" name="size" value="big"></button>
<button type="button" name="size" value="small"></button>
<input type="submit">
and on php page:
<?php
echo You specified:
echo $size;
echo $frame
?>
Many thanks.
The point of a type="button" is to be a thing you can hook JavaScript up to. It isn't designed to send values to the server.
Submit buttons are, but you want to let the user pick from multiple sets rather then just one, so you can't use those either.
While you use buttons with JavaScript that generates/updates hidden inputs with the values you want, you really should just use radio buttons instead. They are designed to let users pick one item from a group.
<form action="processgivenvalues.php" method="post">
<fieldset>
<legend>Colour</legend>
<label>
<input type="radio" name="colour" value="green"> Green
</label>
<label>
<input type="radio" name="colour" value="blue"> Blue
</label>
</fieldset>
<fieldset>
<legend>Size</legend>
<label>
<input type="radio" name="size" value="big"> Big
</label>
<label>
<input type="radio" name="size" value="small"> Small
</label>
</fieldset>
<input type="submit">
</form>
You could use jQuery for this.
html:
choose colour: <button type="button" class="colour" value="green">Green</button>
<button type="button" class="colour" value="blue">Blue</button>
choose size: <button type="button" class="size" value="big">Big</button>
<button type="button" class="size" value="small">Small</button>
<button id='submit' type="button">Submit</button>
jQuery:
You make the form on the run
$(document).ready(function(){
window.size = '';
window.colour = '';
$(".colour").click(function() {
window.colour = $(this).val();
});
$(".size").click(function() {
window.size = $(this).val();
});
$("#submit").click(function() {
if(window.size == '' || window.colour == ''){
return alert('Choose colour and Size');
}
var form = $('<form></form>');
$(form).hide().attr('method','post').attr('action',"processgivenvalues.php");
var input1 = $('<input type="hidden" />').attr('name',"size").val(window.size);
var input2 = $('<input type="hidden" />').attr('name',"colour").val(window.color);
$(form).append(input1);
$(form).append(input2);
$(form).appendTo('body').submit();
});
});
jsFiddle
This can also be done with only javaScript.
As Quentin stated, buttons are not useful for selecting between options, but you comment that you want something customizable with an image...
Option 1: Use <label for="id"></label> with a radio button in a table or list.
<table>
<tbody>
<tr>
<td style="background-color: green;">
<label for="green">
<input name="colour" value="green" id="green" type="radio">
</label>
</td>
</tr>
<tr>
<td style="background-color: blue;">
<label for="blue">
<input name="colour" value="blue" id="blue" type="radio">
</label>
</td>
</tr>
<tr>
<td>
<label for="big">
<input name="size" value="big" id="big" type="radio">
<big>Big</big>
</label>
</td>
</tr>
<tr>
<td>
<label for="small">
<input name="size" value="small" id="small" type="radio">
<small>Small</small>
</label>
</td>
</tr>
</tbody>
</table>
Option2: Use droplists with styling:
<select name="colour2">
<option value="0">Select a color</option>
<option value="green" style="background: green;"> </option>
<option value="blue" style="background: blue;"> </option>
</select>
<br>
<select name="size2">
<option value="0">Select a size</option>
<option value="big" style="font-size: 16px;">Big</option>
<option value="small" style="font-size: 12px;">Small</option>
</select>
Here's a demo fiddle I created (just signed up)
I made my own module to submit a php form's data into the database. I have the module inside of an article now.
When I submit the form, it goes to a 404 error page even though I have another template page for a successful submission.
I have tried using action = post.php as well as the exact URL but it fails.
Does anyone know what I am going wrong?
Here is the URL: http://aubrey-joomla-test.freeiz.com/index.php/new-user-registration-form
Here is my file name for the default template: default_tmpl.php
Here is my tmpl code:
<div>
<form action="<?php echo JRoute::_('index.php'); ?>" method="post" id="myform" class="cols" >
<input type="hidden" name="form_send" value="send" />
<h2>New User Registration Form</h2>
<table>
<label>Are you employed?</label>
<tr>
<td><label><input type="radio" name="option" value="1" required="required">Yes</label></td>
<td><label><input type="radio" name="option" value="0" required="required">No</label></td>
</tr>
<label>If not, please proceed to the next section.</label>
</table>
<fieldset name="salary">
<h4>Income From Employment</h4>
<table> <tr><label>Pay cycle:</label>
<td><label><input type="radio" name="option1" value="1" required="required">Monthly</label></td>
<td><label><input type="radio" name="option1" value="2" required="required">Biweekly</label></td>
</tr> </table>
<p><label>Please enter your typical pay: <input type="text" name="amountpay" required="required" pattern="\d+(\.\d{2})?"/> </label></p>
<p><label>Please select your next pay day: <input type="date" name="paydate" required="required" /> </label></p>
</fieldset>
<fieldset name="fixeditems">
<h4>Fixed Items</h4>
<table> <label>Payment cycle:</label> <tr>
<td><label><input type="radio" name="option2" value="1" required="required">Monthly</label></td>
<td><label><input type="radio" name="option2" value="2" required="required">Biweekly</label></td>
<tr>
</table>
<table> <label>Is this a form of:</label> <tr>
<td><label><input type="radio" name="option3" value="2" required="required">Income</label></td>
<td><label><input type="radio" name="option3" value="1" required="required">Expense</label></td>
<tr>
</table>
<p><label>Please enter the typical amount: <input type="text" name="amount" required="required" pattern ="\d+(\.\d{2})?" /> </label></p>
<p><label>Please select the next due date: <input type="date" name="amountdate" required="required" /> </label></p>
</fieldset>
<hr>
<div class="clear"></div>
<button type="submit" name="send" value="Send">Submit form</button>
<button type="reset">Reset</button>
</form>
</div>
You have no code to handle a form submission. You need to add in a hidden field that would trigger additional code that handles the form submission instead of displaying the form. You need something like this in your module -
if $_POST["form_submit_trigger"] :
PUT SOME CODE HERE TO HANDLE YOUR SUBMITTED DATA
else
PUT THE CODE TO DISPLAY YOUR FORM HERE
form_submit_trigger should be a hidden field in your form that is generated and hashed each time the form is diaplyed. This way you can confirm that the form was actually generated and submitted on your site.