How to print in newline in php? - php

Hello freinds i am unable to print in newline after clicking submit form i am getting all the data in a same row even I have used "\r\n", here is my MWE for html coding
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form name="StudentRegistration" action="proces.php" method="post">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center" cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername" size="30"></td>
</tr>
<tr>
<td>Mother Name</td>
<td><input type="text" name="mothername" id="mothername" size="30"></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="institute">
<option value=" " selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
</select></td>
</tr>
<td>Category</td>
<td><select name="category">
<option value=" " selected>Select..</option>
<option value="Regular">Regular</option>
<option value="Private">Private</option>
</select></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="Male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<td>Stream</td>
<td><select name="stream">
<option value=" " selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="core-1">
<option value=" " selected>Select..</option>
<option value="ENGL">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="core-2">
<option value=" " selected>Select..</option>
<option value="ALTE">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="elective-1">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="elective-2">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="elective-3">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="elective-4">
<option value=" " selected>Select..</option>
<option value="HIST">History</option>
<option value="LOPH">Logic & Philosophy</option>
<option value="MATH">Mathematics</option>
<option value="MBEN">MIL (Bengali)</option>
<option value="MBOD">MIL (Bodo)</option>
<option value="MHIN">MIL (Hindi)</option>
<option value="MNEP">MIL (Nepali)</option>
<option value="PHYS">Physics</option>
<option value="POSC">Political Science</option>
<option value="SOCI">Sociology</option>
<option value="STAT">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="compulsory">
<option value=" " selected>Select..</option>
<option value="ENVE">Environmental Education</option>
</select></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
Here is MWE for php for exporting data to .csv file to save data, I want for every each Submit Form i want to print data in newlines
<?php
$fieldAA = $_POST[" "];
$fieldA = $_POST["textnames"];
$fieldB = $_POST["fathername"];
$fieldC = $_POST["mothername"];
$fieldD = $_POST["institute"];
$fieldE = $_POST["category"];
$fieldF = $_POST["sex"];
$fieldG = $_POST["stream"];
$fieldH = $_POST["core-1"];
$fieldI = $_POST["core-2"];
$fieldJ = $_POST["elective-1"];
$fieldK = $_POST["elective-2"];
$fieldL = $_POST["elective-3"];
$fieldM = $_POST["elective-4"];
$fieldN = $_POST["compulsory"];
$keys = array($fieldAA,$fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,'','',$fieldI,'','',$fieldJ,'','',$fieldK,'','',$fieldL,'','',$fieldM,'','',$fieldN,''); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = $keys;
foreach( $keys as $key ){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'data.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line ;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line ;
fwrite($fcon,$csv_line);
fclose($fcon);
echo "Successfully done! $keys[1]"
?>

In php we can use the PHP_EOL to echo out the end of a line, using this method will not rely on the current document(s)/platforms line endings.
Example:-
$string = "I want a new line here" . PHP_EOL . "This is my new line";
Line breaks in csvs are dependent on what you use to view them also.
Also you probably want to explain what MWE means in your question.

Related

How to export data entry in html form to .csv file?

I am new and beginner and got some codes from internet by searching and editted according to my needs, but now i want to save the data entry in html form to data.csv file, when i click submit button it should save the contents into data.csv in new line for every click submit button. This html codes i am running local computer only. I don't know how do i that here is MWE. Thanking you in advance for any help
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form action="#" name="StudentRegistration" onSubmit="return(validate());">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="30"></td>
</tr>
<tr>
<td>Postal Address</td>
<td><input type="text" name="paddress" id="paddress" size="30"></td>
</tr>
<tr>
<td>Personal Address</td>
<td><input type="text" name="personaladdress"
id="personaladdress" size="30"></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Stream</td>
<td><select name="Stream">
<option value="-1" selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="Core-1">
<option value="-1" selected>Select..</option>
<option value="English">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="Core-2">
<option value="-1" selected>Select..</option>
<option value="Alternative English">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="Elective-1">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="Elective-2">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="Elective-3">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="Elective-4">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="Compulsory">
<option value="-1" selected>Select..</option>
<option value="Environmental Education">Environmental Education</option>
</select></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="Institute">
<option value="-1" selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
<option value="Institute-3">Institute-3</option>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select Name="State">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Bihar">BIHAR</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode" size="30"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid" size="30"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="30"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno" size="30"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
Here is the output of the above html codes
I have created a .php as proces.php file according to Bill suggestion but content not writing but giving only ,,,,,,, here is my php script
<?php
$fieldA = $_POST["textnames"];
$fieldB = $_POST["fathername"];
$fieldC = $_POST["paddress"];
$fieldD = $_POST["personaladdress"];
$fieldE = $_POST["sex"];
$fieldF = $_POST["Stream"];
$fieldG = $_POST["Core-1"];
$fieldH = $_POST["Core-2"];
$fieldI = $_POST["Elective-1"];
$fieldJ = $_POST["Elective-2"];
$fieldK = $_POST["Elective-3"];
$fieldL = $_POST["Elective-4"];
$fieldM = $_POST["Compulsory"];
$keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'data.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$fcontent);
fclose($fcon);
?>
and edited my original html codes from <form action="#" name="StudentRegistration" onSubmit="return(validate());"> to <form name="StudentRegistration" action="proces1.php" method="post"> so here is my new final html coding
<html>
<head>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form name="StudentRegistration" action="proces.php" method="post">
<table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Student Registration Form</b></font></center>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type=text name=textnames id="textname" size="30"></td>
</tr>
<tr>
<td>Father Name</td>
<td><input type="text" name="fathername" id="fathername"
size="30"></td>
</tr>
<tr>
<td>Postal Address</td>
<td><input type="text" name="paddress" id="paddress" size="30"></td>
</tr>
<tr>
<td>Personal Address</td>
<td><input type="text" name="personaladdress"
id="personaladdress" size="30"></td>
</tr>
<tr>
<td>Sex</td>
<td><input type="radio" name="sex" value="male" size="10">Male
<input type="radio" name="sex" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Stream</td>
<td><select name="Stream">
<option value="-1" selected>Select..</option>
<option value="ARTS">ARTS</option>
<option value="COMMERCE">COMMERCE</option>
<option value="SCIENCE">SCIENCE</option>
</select></td>
</tr>
<tr>
<td>Core-1</td>
<td><select name="Core-1">
<option value="-1" selected>Select..</option>
<option value="English">English</option>
</select></td>
</tr>
<tr>
<td>Core-2</td>
<td><select name="Core-2">
<option value="-1" selected>Select..</option>
<option value="Alternative English">Alternative English</option>
</select></td>
</tr>
<tr>
<td>Elective-1</td>
<td><select name="Elective-1">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-2</td>
<td><select name="Elective-2">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-3</td>
<td><select name="Elective-3">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Elective-4</td>
<td><select name="Elective-4">
<option value="-1" selected>Select..</option>
<option value="MIL (Hindi)">MIL (Hindi)</option>
<option value="MIL (Bengali)">MIL (Bengali)</option>
<option value="MIL (Nepali)">MIL (Nepali)</option>
<option value="Political Science">Political Science</option>
<option value="Education">Education</option>
<option value="Economics">Economics</option>
<option value="History">History</option>
<option value="Geography">Geography</option>
<option value="Sociology">Sociology</option>
<option value="Logic & Philosophy">Logic & Philosophy</option>
<option value="Advance Assamese">Advance Assamese</option>
<option value="Advance Bengali">Advance Bengali</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
<option value="Biology">Biology</option>
<option value="Mathematics">Mathematics</option>
<option value="Computer Science & Applications">Computer Science & Applications</option>
<option value="Accountancy">Accountancy</option>
<option value="Business Studies">Business Studies</option>
<option value="Banking">Banking</option>
<option value="Statistics">Statistics</option>
</select></td>
</tr>
<tr>
<td>Compulsory</td>
<td><select name="Compulsory">
<option value="-1" selected>Select..</option>
<option value="Environmental Education">Environmental Education</option>
</select></td>
</tr>
<tr>
<td>Institute</td>
<td><select name="Institute">
<option value="-1" selected>Select..</option>
<option value="Institute-1">Institute-1</option>
<option value="Institute-2">Institute-2</option>
<option value="Institute-3">Institute-3</option>
</select></td>
</tr>
<tr>
<td>State</td>
<td><select Name="State">
<option value="-1" selected>select..</option>
<option value="New Delhi">NEW DELHI</option>
<option value="Mumbai">MUMBAI</option>
<option value="Goa">GOA</option>
<option value="Bihar">BIHAR</option>
</select></td>
</tr>
<tr>
<td>PinCode</td>
<td><input type="text" name="pincode" id="pincode" size="30"></td>
</tr>
<tr>
<td>EmailId</td>
<td><input type="text" name="emailid" id="emailid" size="30"></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="dob" id="dob" size="30"></td>
</tr>
<tr>
<td>MobileNo</td>
<td><input type="text" name="mobileno" id="mobileno" size="30"></td>
</tr>
<tr>
<td><input type="reset"></td>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
This post should get you where you need to go...
How to export html form to csv file
The answer there gives a good explanation on how the script works.
edit:
You're going to need to create a PHP file and pass the data when you hit submit FORM should look like this notice the POST and .php file.
<form action="NAMEOFFILE.php" method="post">
NAMEOFFILE.PHP will contain the form data and the export to CSV script in the link attached. If are not sure how to pass the data this is a straightforward explanation. https://www.tutorialrepublic.com/php-tutorial/php-get-and-post.php
<?php
$fieldA = $_POST["textnames"];
$keys = array('id_number','first_name'); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'file_to_write_to.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line);
fclose($fcon);
?>
The file will be saved on your server with the name "file_to_write_to.csv"
Thanks to everyone Now I finally got the output after editing the proces.php script by changing the line from $csv_line = array(); to $csv_line = $keys; and from if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;} to if(!file_exists($fname)){$csv_line = $csv_line."\r\n" ;} here is my final .php file
<?php
$fieldA = $_POST["textnames"];
$fieldB = $_POST["fathername"];
$fieldC = $_POST["paddress"];
$fieldD = $_POST["personaladdress"];
$fieldE = $_POST["sex"];
$fieldF = $_POST["Stream"];
$fieldG = $_POST["Core-1"];
$fieldH = $_POST["Core-2"];
$fieldI = $_POST["Elective-1"];
$fieldJ = $_POST["Elective-2"];
$fieldK = $_POST["Elective-3"];
$fieldL = $_POST["Elective-4"];
$fieldM = $_POST["Compulsory"];
$keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = $keys;
foreach( $keys as $key ){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'data.csv'; //NAME OF THE FILE
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = $csv_line."\r\n" ;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line);
fclose($fcon);
?>

SQL Insert Statement Inserting 2 records instead of 1

I've seen some people have the same issue, but different scenarios. And searching just gives me 'how to use joins'.
When I execute this through my php page, two records are inserted. One with the content, and one with just the date. I can't seem to find where the date field is being executed. I've had this issue in the past, but with a past version of mySQL and with depreciated values, so using the new values is someone newer to me.
Below is my php and sql statement:
<?php
$link = mysqli_connect('server', 'username', 'password');
if (!$link) {
die('Could Not Connect: ' . mysqli_error());
}
else echo 'Connected Succesffully';
mysqli_select_db($link, 'database');
mysqli_query($link, "INSERT INTO tblcc
(date , production , productionco , city , state ,
genre , type , details , part1 , gender1 ,
char1 , part2 , gender2 , char2 , part3 ,
gender3 , char3 , part4 , gender4 , char4 , part5 ,
gender5 , char5)
VALUES(now() , '$_POST[production]' , '$_POST[productionco]' , '$_POST[city]' , '$_POST[state]' ,
'$_POST[genre]' , '$_POST[type]' , '$_POST[details]' , '$_POST[part1]' , '$_POST[gender1]' ,
'$_POST[char1]' , '$_POST[part2]' , '$_POST[gender2]' , '$_POST[char2]' , '$_POST[part3]' ,
'$_POST[gender3]' , '$_POST[char3]' , '$_POST[part4]' , '$_POST[gender4]' , '$_POST[char4]' ,
'$_POST[part5]' , '$_POST[gender5]', $_POST[char5]')");
mysqli_close($link);
?>
The HTML:
<form action="http://enhstudios.com/bc1/submitcc.php/" method="post">
<table border="3" cellpadding="3" cellspacing="3">
<tr>
<th>Production:</th>
<th>Production Company:</th>
<th colspan="2">Location:</th>
</tr>
<tr>
<td><input type="text" name="production"></td>
<td><input type="text" name="productionco"></td>
<td><input type="text" name="city"></td>
<td><select name="state">
<option value=""></option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kentucky">Kentucky</option>
<option value="Missouri">Missouri</option>
<option value="Ohio">Ohio</option>
<option value="Wisconsin">Wisconsin</option>
</select>
</td>
</tr>
<tr>
<th>Genre:</th>
<th>Type:</th>
<th colspan="2">Details:</th>
</tr>
<tr>
<td><select name="genre">
<option value=""></option>
<option value="Action">Action</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
<option value="Horror">Horror</option>
<option value="Romance">Romance</option>
</select>
</td>
<td><select name="type">
<option value=""></option>
<option value="Feature Length">Feature Length</option>
<option value="Mini-Series">Mini-Series</option>
<option value="Short">Short</option>
<option value="Web Series">Web Series</option>
</select>
</td>
<td colspan="2"><textarea maxlength="255" rows="5" cols="30" name="details"></textarea></td>
</tr>
<tr>
<th colspan="4">Parts:</th>
</tr>
<tr>
<th>Role Type:</th>
<th>Gender:</th>
<th colspan="2">Character:</th>
</tr>
<tr>
<td><select name="part1">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender1">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char1">
</td>
</tr>
<tr>
<td><select name="part2">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender2">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char2">
</td>
</tr>
<tr>
<td><select name="part3">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender3">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char3">
</td>
</tr>
<tr>
<td><select name="part4">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender4">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char4">
</td>
</tr>
<tr>
<td><select name="part5">
<option value=""></option>
<option value="Extra">Extra</option>
<option value="Leading">Leading</option>
<option value="Supporting">Supporting</option>
</select>
</td>
<td><select name="gender5">
<option value=""></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
</td>
<td colspan="2"><input type="text" name="char5">
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td colspan="3"></td>
</tr>
</table>
</form>
I found the "issue". I nestled the form inside the table instead of the table inside the form.

Value from drop down list html

Trying to pass the value from Drop down list through but couldnt get it.
<tr>
<td>Size <br>
(Only applicable for T-Shirt):</td>
<td><select name=size>
<option value=N>NIL</option>
<option value=S>S</option>
<option value=M>M</option>
<option value=L>L</option>
<option value=XL>XL</option>
</select></td>
</tr>
<tr>
<td></td>
<td><a href= manageProduct.php?size=$_GET[size]>Insert</a></td>
</tr>
Give this a try: (just an example).
<form action="<?php $_SERVER['PHP_SELF']?>" method="GET">
<table>
<tr>
<td nowrap>Size:
(Only applicable for T-Shirt):</td>
<td><select name="size">
<option value="N">NIL</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select></td>
</tr>
<tr>
<td><p align="right"></p></td>
<td><input type="submit" value="Insert" />
</td>
</tr>
</table>
</form>
<?php
$size = $_GET['size'];
if($_GET['size']=="N")
{
echo "you chose NOTHING, make another choice.";
}
else {
echo "Your choice of size is: $size";
}
?>
Html
<form action="manageProduct.php" method="POST">
<table>
<tr>
<td>Size <br>
(Only applicable for T-Shirt):</td>
<td><select name=size>
<option value=N>NIL</option>
<option value=S>S</option>
<option value=M>M</option>
<option value=L>L</option>
<option value=XL>XL</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Insert" /></td>
</tr>
</table>
</form>
PHP
$size = $_POST['size'];
You need a form using the GET action then you can get your variable.
Assuming this page is the manageProduct.php:
<!--GET your data from the submitted form OR do something depending on what size returns in the url-->
<?php
if($_GET['size']=="SIZE"){ echo whatever or do whatever}
?>
<td>Whatever you're trying to do</td>
<form id="form" method="get" action="manageProduct.php">
<tr>
<td>Size <br>
(Only applicable for T-Shirt):</td>
<td><select name="size">
<option value="N">NIL</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Insert" /></td>
<br />
</form>
</tr>

website moved and php form not working (old php version)

I moved website from to other hosting server and contact form doesn't work. Current version of php is 5.2 and before was older. I saw on forums that in order for older php code to work i should turn globals on which I did (I know it's insecure and I will replace that code completely, but right now I have to make it work). Before I turned globals on nothing was working and now I'm getting message if some field is missing but nothing more than that. Any suggestions how to make this contact form work? Any answer will be very appreciated.
<?
$contact = 'contact';
require('inc_meta.php');
if($form)
{
$error = Array();
if(!trim($c_name)){ $error[sizeof($error)] = "Name"; }
if(!trim($email)){ $error[sizeof($error)] = "E-mail Address"; }
else {
if(!eregi("^[_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)){ $error[sizeof($error)] = "'" . $email . "' is not avalid E-mail Address."; }
}
}
$filePath = getenv("SCRIPT_NAME");
if($_SERVER['HTTP_REFERER'] == 'http://'.$_SERVER['SERVER_NAME'].''.$filePath.'')
{
if($form && !$error)
{
$contents = "Name: $c_name\r
Address:\r
$c_address\r
$c_city, $c_state $c_zip\n
Phone: $c_phone\r
Fax: $c_fax\r
Email: $email\n
Comments or Questions:\r
$c_comments";
mail("$EMAILADMIN","email from $c_name","$contents","From: $c_name<$email>");
$status = "Thank You, <b>$c_name</b><br><br>";
$status .= "Your e-mail message has been sent. We will contact you shortly about your inquiry. ";
$status .= "If you need more assistance or have questions, please call us at <b>1234567</b><br><br>";
$status .= "Thank You,<br><b>Owner</b><br>";
}}
?>
<table width="860" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<td width="325">
<? include('inc_sideBar.php'); ?>
</td>
<td width="535">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<? include('inc_header.php'); ?>
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td align="center" valign="top">
<? if(!$status){ ?>
<br>
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<form name="form" method="post" action="<? echo $PHP_SELF; ?>">
<td>
<? if(sizeof($error)){ ?>
<font color="#FADE82"><b> The following fields are still
required:<br>
<?
for($i = 0; $i < sizeof($error); $i++) {
echo " *", " ", $error[$i];
}
?>
</b></font><br>
<br>
<? } ?>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="54%" valign="top"><font color="#FFF9DD">Name</font><font color="#CC0000">
<font color="#FADE82"></font></font><b><font color="#CC0000"><font color="#FADE82">*</font><br>
<input type="text" name="c_name" value="<? echo $c_name; ?>" size="23">
</font></b></td>
<td width="46%" valign="top"><font color="#FFF9DD">State</font><b><br>
</b>
<select name="c_state">
<option>
<? echo $c_state; ?>
</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="DC">Washington, D.C.</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</td>
</tr>
<tr>
<td valign="top" width="54%"><font color="#FFF9DD">E-mail</font><font color="#CC0000">
<font color="#FADE82"></font></font><b><font color="#CC0000"><font color="#FADE82">*</font>
<br>
<input type="text" name="email" value="<? echo $email; ?>" size="23">
</font></b></td>
<td valign="top" width="46%"><font color="#FFF9DD">Zip</font><b><br>
</b>
<input type="text" name="c_zip" value="<? echo $c_zip; ?>" size="20">
</td>
</tr>
<tr>
<td valign="top" width="54%"><font color="#FFF9DD">Address</font><b><br>
<input type="text" name="c_address" value="<? echo $c_address; ?>" size="23">
</b></td>
<td valign="top" width="46%"><font color="#FFF9DD">Phone</font><b><br>
</b>
<input type="text" name="c_phone" value="<? echo $c_phone; ?>" size="20">
</td>
</tr>
<tr>
<td valign="top" width="54%"><font color="#FFF9DD">City</font><b><br>
<input type="text" name="c_city" value="<? echo $c_city; ?>" size="23">
</b></td>
<td valign="top" width="46%"><font color="#FFF9DD">Fax</font><b><br>
</b>
<input type="text" name="c_fax" value="<? echo $c_fax; ?>" size="20">
</td>
</tr>
<tr>
<td valign="top" width="54%"> </td>
<td valign="top" width="46%"> </td>
</tr>
<tr>
<td valign="top" colspan="2"> <font color="#FFF9DD">Comments
or Questions?:</font><br>
<textarea name="c_comments" wrap="VIRTUAL" cols="30" rows="5"><? echo $c_comments; ?></textarea>
<br>
<br>
</td>
</tr>
<tr>
<td valign="top" colspan="2">
<input type="image" border="0" name="imageField" src="images/submit.gif" class="select">
</td>
</tr>
</table>
<input type="hidden" name="form" value="one">
</td>
</form>
</tr>
</table>
<? }else{ ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> <br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<font color="#FFF9DD"><? echo $status; ?></font>
</td>
</tr>
</table>
</td>
</tr>
</table>
<? } ?>
</td>
</tr>
</table>
<br>
<? include('footer_info.php'); ?>
</td>
</tr>
</table>
</td>
</tr>
</table>
<? require('inc_footer.php'); ?>
You should access the form data using the $_POST array.
Read the Warning in this page of the php docs: http://www.php.net/manual/en/language.variables.predefined.php

HTML number of rows variables on MySQL column value

I'm trying to figure out how to make a query that sets the rows (number of weeks) depending on the number of weeks the instructor wants the clinical to go, this value is stored in a Mysql table called clinical with a column name weeks (int). Right now in my HTML I have just pasted 4 rows to get the look down. I realize I will need to make a connection to the db before this code
<table width="1000" border="1">
<tr>
<td colspan="8" style="background-color:#1589FF;text-align:center;">
<h1>Rating for the Week</h1>
</td>
<tr>
<th>Week</th>
<th>Faculty Signature</th>
<th>Rating for the Week <br>
S+/S/NI/U</th>
<th>Student Signature</th>
<th>Rating for the Week <br>
S+/S/NI/U</th>
</tr>
<tr>
<tr ALIGN="CENTER">
<td>1</td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
</tr>
<tr>
<tr ALIGN="CENTER">
<td>2</td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
</tr>
<tr>
<tr ALIGN="CENTER">
<td>3</td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
</tr>
<tr>
<tr ALIGN="CENTER">
<td>4</td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
<td><form><input type = "submit" /></form></td>
<td><select>
<option value="S+">S+</option>
<option value="S">S</option>
<option value="NI">NI</option>
<option value="U">U</option>
</select></td>
</tr>
<tr>
<td colspan="8" style="background-color:#1589FF;text-align:center;">
<b> The student must pass the clinical component of the course which is ted on a pass/fail basis.<br>
A clinical failure results in a grade of F for the course.<b>
</td>
</tr>
</table>
</body>
</html>
The query would look like this:
SELECT weeks FROM `clinical` WHERE clinicname = $clinicname
The query will select the column weeks from the table clinical
I added the WHERE statement because if there are multiple clinics it would choose the appropriate one, as long as you have the column clinicname included in your table.
To make a row for each week you would create a while loop like so:
<?php
$number_of_weeks_from_query = $query_result;
$i = 0;
while($i < $number_of_weeks_from_query){
INSERT EACH ROW TEMPLATE HERE AND CHANGE THE WEEK NUMBER TO $i
$i++;
}
?>
I would suggest using Mysql PDO as the old MySql functions will be deprecated very soon. I would also suggest to use AJAX so you can keep the page loaded when someone hits the submit button. Please tell me if I did anything wrong, my PHP is still rusty.

Categories