HTML into a text file using PHP - php

I am trying to get this information into a text file using PHP.
All of the websites that I have looked at only really cover entering a first and a last name had having the information echoed.
I was wondering if anyone has a suggestion on how to do this? That would be very helpful.
<form action="action.php" method="post" />
Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>
If yes, please indicate which of these currently purchased toolboxes you use $
form action="action.php" method="post" />
Please indicate if you us Mathworks MATLAB: <br>
<input type="radio" name="question" value="yes" checked> Yes<br>
<input type="radio" name="question" value="no"> No<br>
If yes, please indicate which of these currently purchased toolboxes you use $
<input type="checkbox" name="tool" value="Control">Control Systems Toolbox<br>
<input type="checkbox" name="tool" value="Image">Image Processing Toolbox<br>
<input type="checkbox" name="tool" value="Optimiz">Optimization Toolbox<br>
<input type="checkbox" name="tool" value="Robust">Robust Control Toolbox<br>
<input type="checkbox" name="tool" value="Signal">Signal Processing Toolbox<br>
Please enter a comma separated list of toolboxes you would like to use for you$
<input type="text" name="textquestion" value=""><br>
<input type="submit" value="Submit">

This is fairly straightforward,
$file = "myfile.txt";
if (isset($_POST['question']) && isset($_POST['tool'])) {
$fh = fopen($file, 'a');
$text = $_POST['question']. ' ' .$_POST['tool'];
fwrite($fh,$text); // Write form data to the file
fclose($fh); // Close the file
}
This would be put in your action.php file, and would write to a file in the same directory.
Also a caveat, you have input names that aren't unique, so you would want to add a third field to this. 'question' should be a unique name as you're using it when posting data.

Related

return state of a checkbox and return 0 if unchecked

I have been finding interesting possible solutions to this, none speak to me clearly unfortunately.
What I have is a series of checkboxes that store the users preferences, so when the page populates I need to know the state of the checkbox so it can display the preference. Also I noticed that HTML doesnt return a value if the checkbox is not checked, well I need to store a 0 if it is not checked. So what is a good way to accomplish this? I am using just HTML and PHP
I have seen a few solutions and have some questions. First the code
<form action="TradingRules.php" method="post">
<h2><legend>Entry Rules</legend></h2>
<p><label> <input type="checkbox" name="30minRule" value="1" <?php if(isset($_POST["30minRule"])) { echo 'checked="checked" '; } ?>>30 Min Rule</label></p>
<p><label> <input type="checkbox" name="buyOnPocketPivot" value="1">Buy on Pocket Pivot</label></p>
<p><label> <input type="checkbox" name="buyEODonPPday" value="1">Buy EOD on PP Day</label></p>
<p><label> <input type="checkbox" name="buyOn10WeekLineTest" value="1">Buy on 10 week line Test (within 3%)</label></p>
<p><label> <input type="checkbox" name="okBelow400k" value="1">Ok below 400k Average</label></p>
<p><label> <input type="checkbox" name="buyIfOthersAreBreakingOut" value="1">Buy if other are breaking out</label></p>
<p><label> <input type="checkbox" name="buyIfStockHasUnusualStrength" value="1">Buy if stock has unusual strength</label></p>
<p><label> <input type="checkbox" name="buyOnNon-breakoutVolume" value="1">Buy on non-breakout Volume</label></p>
<p><label> <input type="checkbox" name="buyOnGap" value="1">Buy on Gap</label></p>
<p><label> <input type="checkbox" name="enableGapRules" value="1">Enable Gap Rules</label></p>
<p><label> <input type="checkbox" name="buy<1WeekBeforeEarnings" value="1">Buy < 1 week before earning</label></p>
<p><label> <input type="checkbox" name="buyOn3WeeksTight" value="1">Buy on 3 weeks tight</label></p>
<p><label> <input type="checkbox" name="buyOnHTF" value="1">Buy on HTF</label></p>
<p><label> <input type="checkbox" name="buyOnPullbackToPivot" value="1">Buy on pullback to pivot </label></p>
<p><input type="submit" /></p>
</form>
Questions:
1) where do you put the hidden button? I have <p> tags because I want them on top of the other so do I put them all in the <p> tags? So would something like this work?
<p><label> <input type="hidden" name="30minRule" value="0">
<input type="checkbox" name="30minRule" value="1" <?php if(isset($_POST["30minRule"])) { echo 'checked="checked" '; } ?>>30 Min Rule</label></p>
<p><label> <input type="hidden" name="buyOnPocketPivot" value="0">
<input type="checkbox" name="buyOnPocketPivot" value="1">Buy on Pocket Pivot</label></p>
2) On the topic of getting the info back from the database for checkbox status, I have the form linked to my php script will that be adequate for getting those values? They all go to and from the same location, or do I need something else?
Thanks in advance for the help!
When you receive the value in PHP, it might be worth it to just make whatever is missing into a 0. This is easy enough with the array_merge function.
$post_form = array_merge(array(
'30minRule' => 0,
'buyOnPocketPivot' => 0 // etc
), $_POST);

Form Check boxes not coming through

I can not get the form check boxes to come through to the email for "recycling". I do not know php so I have no idea what is wrong. When the form comes through to email the "Recycling:" subject is there, but the checked boxes are not.
HTML:
<label>CRT Monitors <input name="recycleobject2[]" type="checkbox" value="crtmonitors"
/></label>
<label>Printers <input name="recycleobject2[]" type="checkbox" value="printers"
/></label>
<label>Computers <input name="recycleobject2[]" type="checkbox" value="computers"
/></label>
<label>Fluorescent Lamps and Batteries <input name="recycleobject2[]" type="checkbox"
value="lamps" /></label>
<label>Televisions <input name="recycleobject2[]" type="checkbox" value="television"
/></label>
<label>Other Equipment <input name="recycleobject2[]" type="checkbox" value="other" />
</label><br />
PHP:
$recycleobject2 = $_POST['recycleobject2']; // not required
$recycleobject2 = array();
$email_message .= "Recycling:" .implode(", ",$recycleobject2)."\n";
your problem is right here:
$recycleobject2 = array();
That line is resetting the $recycleobject2 variable to a brand new empty array. Remove that line and your code should be fine.

PHP error - Inserting a user form into a database

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);
?>

php comparison of values in two radio buttons

this is one radio button set
<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>
and so on...
and this is another radio button set
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600
my question is how can i compare value of one with value="100" to other with id="100" in php
there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database
means..
there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse
still i have to do comparison of both.. and that too serverside
so what are my options?
more detailed explanation....
there are two sets of radio buttons..
in one set there are values..
and in other sets there are images related to those values..
when user clicks set of value... and user click on set of images
so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200
database shall not allow it to enter and gave him some error
Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'
<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>
<?php
$image = strstr($_POST['image'],'_');
$number = '_'.$_POST['number'];
if($image == $number) {
//Do something
}
The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.
This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.
Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.
You need to take an entirely different approach.
[EDITED after question edit]
Add the 100 or 200, etc to the value of the image radio buttons, like so:
<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>
(The number radio button set remains unchanged from your question)
You can then read it in PHP as follows:
$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);
You can now compare $number with $imgNumber.
Hope that helps.
It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.
This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.

Many fields in HTML form

I have a form with about 150 fields, however the user can add more fields, reaching about 300 fields.
I'd like to know if exist a good way to put names and IDs to my fields, or i have to give each one a diferent name and ID. It's a hard job.
html with only one name for input
<form action="checkbox.php">
<input type="checkbox" name="data[]" value="1">1<br>
<input type="checkbox" name="data[]" value="2">2<br>
<input type="checkbox" name="data[]" value="3">3<br>
<input type="checkbox" name="data[]" value="4">4<br>
<input type="checkbox" name="data[]" value="5">5<br>
<input type="submit">
</form>
checkbox.php
$data = $_GET["data"]; //$data is an array with checked option
it works also for type text
<input type="text" name="text_data[1]"><br>
<input type="text" name="text_data[2]"><br>
<input type="text" name="text_data[3]"><br>
if i understand in right, then you can use same name,id for group of same types of control (group of textbox,checkbox,etc...) and then access as array from javascript or code behind ..

Categories