I am making a poll with HTML and PHP and I want to post the poll answers in a text file. I have 6 things to choose from.
The name is answer and the values are a1, a2, a3, a4, a6. How to post a1 or a2 or a3... in the file.
You know when you click on the answer with id a1 to post in a new line a1 in the file.
HTML:
<form action="php/vote.php">
<b><strong>Vote:</strong></b> <br>
<input type="radio" name="answer" value="a1" id="a1">a<br>
<input type="radio" name="answer" value="a2" id="a2">b<br>
<input type="radio" name="answer" value="a3" id="a3">c<br>
<input type="radio" name="answer" value="a4" id="a4">d<br>
<input type="radio" name="answer" value="a5" id="a5">e
<br>
<input type="submit" name="submit" id="submit" value="Vote">
</form>
This should do what you are asking:
<?php
if(isset($_POST['answer'])){
file_put_contents("filename.txt", $_POST['answer']."\n");
}
?>
<html>
<head>
</head>
<body>
<form action="" method="POST">
<b><strong>Vote:</strong></b> <br>
<input type="radio" name="answer" value="a1" id="a1">a<br>
<input type="radio" name="answer" value="a2" id="a2">b<br>
<input type="radio" name="answer" value="a3" id="a3">c<br>
<input type="radio" name="answer" value="a4" id="a4">d<br>
<input type="radio" name="answer" value="a5" id="a5">e
<br>
<input type="submit" name="submit" id="submit" value="Vote">
</body>
</form>
</html>
I have added method="POST" to your original HTML so that the receiving PHP can inspect the $_POST variable to see what was sent through from the form. $_POST['answer'] contains the value of the "value" field corresponding to the selected radio button. Once you know this, it is easy to call file_put_contents to write that value to your file. Append a newline "\n" to ensure that each call writes to a separate line of the file.
Related
I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />
<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;
}
?>
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);
});
How to read to variable which button is clicked.
I have this five button's which are like answers.
<fieldset> <legend> Question1 </legend>
<input type="button" value="1st Ansswer"/>
<input type="button" value="2nd Ansswer"/>
<input type="button" value="3rd Ansswer"/>
<input type="button" value="4th Ansswer"/>
<input type="button" value="5th Ansswer"/>
</fieldset>
I want to read the answer for question 1 into variable to send it via email.
For better explain i want something like this
This is for
<select name="SOption"><option>Option1</option><option>Option2</option></select>
I read the result in variable Question like this
$Question= $_POST['SOption'];
How to do the same with button's instead select.
Hope you understand
<fieldset>
<legend>
Question1
</legend>
<input type="button" name="foo" value="1st Ansswer"/>
</fieldset>
$Question= $_POST['foo'];
Just add a name or ID to each button whose value you want.
Why cant you just use radio buttons??
<fieldset> <legend> Question1 </legend>
<input type="radio" name="radio_q1" value="1st Ansswer"/>
<input type="radio" name="radio_q1" value="2nd Ansswer"/>
<input type="radio" name="radio_q1" value="3rd Ansswer"/>
<input type="radio" name="radio_q1" value="4th Ansswer"/>
<input type="radio" name="radio_q1" value="5th Ansswer"/>
</fieldset>
<fieldset> <legend> Question2 </legend>
<input type="radio" name="radio_q2" value="1st Ansswer"/>
<input type="radio" name="radio_q2" value="2nd Ansswer"/>
<input type="radio" name="radio_q2" value="3rd Ansswer"/>
<input type="radio" name="radio_q2" value="4th Ansswer"/>
<input type="radio" name="radio_q2" value="5th Ansswer"/>
</fieldset>
<input type="submit" value="SUBMIT">
Otherwise another way would be with a hidden text field, and javscript/jquery.
Like so....
<form id='theForm' method='POST' action='wherever'>
<input type='hidden' name='thequestion1' id='thequestion1'>
</form>
<button class='questionButton' data-answer='TheAnswer 1'>Answer 1</button>
<button class='questionButton' data-answer='TheAnswer 2'>Answer 2 </button>
<button class='questionButton' data-answer='TheAnswer 3'>Answer 3 </button>
<button class='questionButton' data-answer='TheAnswer 4'>Answer 4 </button>
Then your jquery..
$(document).ready,function(){
$('.questionButton').click(function(e){
e.preventDefault();
var answer = $(this).data('answer');
$('#thequestion1').val(answer);
$('#theForm').submit();
});
});
And then from this, you can just extrapolate how you would do multiple questions, store them all in the hidden text fields for each question, then submit the form in the end.
I managed to do this with hiden button
function change_value($id, $value)
{
document.getElementById($id).value = $value;
}
<input type='button' class="myButton2" onclick="change_value('quest1', this.value);" value="Ans1">
<input type='button' class="myButton3" onclick="change_value('quest1', this.value);" value="Ans2">
<input type='button' class="myButton4" onclick="change_value('quest1', this.value);" value="Ans3">
<input type='button' class="myButton5" onclick="change_value('quest1', this.value);"value="Ans4">
I'm getting a strange error when I try to submit user-generated data to a database via PHP commands. When I hit the submit button below, instead of the PHP page running its' function I am presented with a display of the raw code on my browser. I have a command at the bottom of my HTML page that looks like this:
<form action="insert.php" method="post">
<input type="submit">
</form>
So that when the user hits the submit button, the PHP file insert.php (detailed below) is called to input the answers onto a database, separating each answer into it's own field.
Here is the code I'm working with:
<?php
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Right now, the questions are in a and not a (is there a functional difference in this case?). They look like:
<form class="testAns" id="widthAns">
<input type="radio" name="answer" value="skinny">-25%
<input type="radio" name="answer" value="skinny">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="fat">+10%
<input type="radio" name="answer" value="fat">+25%
</form>
<form class="testAns" id="spaceAns">
<input type="radio" name="answer" value="small">-25%
<input type="radio" name="answer" value="small">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="wide">+10%
<input type="radio" name="answer" value="wide">+25%
</form>
<form class="testAns" id="weightAns">
<input type="radio" name="wanswer" value="light">-25%
<input type="radio" name="answer" value="light">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="heavy">+10%
<input type="radio" name="answer" value="heavy">+25%
</form>
<form method="post" action="insert.php" class="testAns" id="heightAns">
<input type="radio" name="answer" value="short">-25%
<input type="radio" name="answer" value="short">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="tall">+10%
<input type="radio" name="answer" value="tall">+25%
</form>
The important part is for the "value" associated with each button to be logged into the database. For example, if a user selects "+10%" I want be able to log the word "heavy".And then there are two text input fields:
<form id="intro">
City: <input type="text" name="answer"><br>
Why you are using this tool:<input type="text" name="answer">
</form>
So for these text fields I need the user input logged as the answer.
I see you got the PHP thing fixed.
Now you need to fill your form with data. This:
<form action="insert.php" method="post">
<input type="submit">
</form>
sends only the submit value. You need to add input fields inside that form tag, otherwise, nothing else will get sent. So, since you're sending an answer array, you should add those (adding them as text fields, as an example):
<form action="insert.php" method="post">
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
etc...
<input type="submit" />
</form>
And make sure you filter all user inputs before writing anything into the database, as otherwise my buddy Bobby Tables might come to visit you.
Make sure in your XAMPP Control Panel that Apache and MySQL are running. Then check if your input fields are inside the <form action='insert.php' method='POST'> input fields </form>
Your HTML code would look like this:
<html>
<body>
<form action='insert.php' method='POST'>
<table>
<tr><td>Width: </td><td>
<input type="radio" name="width" value="skinny">-25%
<input type="radio" name="width" value="skinny">-10%
<input type="radio" name="width" value="mid">normal
<input type="radio" name="width" value="fat">+10%
<input type="radio" name="width" value="fat">+25%
</td></tr>
<tr><td>Spacing: </td><td>
<input type="radio" name="spacing" value="small">-25%
<input type="radio" name="spacing" value="small">-10%
<input type="radio" name="spacing" value="mid">normal
<input type="radio" name="spacing" value="wide">+10%
<input type="radio" name="spacing" value="wide">+25%
</td></tr>
<tr><td>Weight: </td><td>
<input type="radio" name="weight" value="light">-25%
<input type="radio" name="weight" value="light">-10%
<input type="radio" name="weight" value="mid">normal
<input type="radio" name="weight" value="heavy">+10%
<input type="radio" name="weight" value="heavy">+25%
</td></tr>
<tr><td>Height: </td><td>
<input type="radio" name="height" value="short">-25%
<input type="radio" name="height" value="short">-10%
<input type="radio" name="height" value="mid">normal
<input type="radio" name="height" value="tall">+10%
<input type="radio" name="height" value="tall">+25%
</td></tr>
<tr><td>City: </td><td><input type="text" name="city"></td></tr>
<tr><td>Why you are using this tool: </td><td><input type="text" name="tool"></td></tr>
<tr><td></td><td><input type='submit'></td></tr>
</table>
</form>
</body>
</html>
What are you using in creating your php files? Dreamweaver? Notepad? Try this: SAVE AS your file, Save As Type: All Files and name it insert.php.
<?php
$con=mysqli_connect("localhost","YourUsername","YourPassword(if any)","NameOfYourDatabase");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$width=$_POST['width'];
$spacing=$_POST['spacing'];
$weight=$_POST['weight'];
$height=$_POST['height'];
$city=mysqli_real_escape_string($con,$_POST['city']);
$tool=mysqli_real_escape_string($con,$_POST['tool']);
/* REAL ESCAPE STRING WOULD PREVENT A BIT OF SQL INJECTION */
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$city','$tool','$width','$height','$spacing','$weight')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>