I am trying to create a simple online form using html and php tooutput to a .csv. I have no problem doing this for a single form but the users of this form will usually have multiple entries at once. To make it easier for them I am using a form of which you can add lines to submit more entries in one submission. Below is the HTML and PHP code.
The issue I am having is the PHP part. I can only get it to ever submit the first entry.
Any ideas? I have a working version of the site/form here, but again, only the first entry every gets entered into the .csv. Thanks for the help.
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
var currentItem = 0;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td>Area:<input class="textfield" name="area'+currentItem+'" id ="area'+currentItem+'"type="text" /></td><td>Contractor:<input class="textfield" name="contractor'+currentItem+'" id ="contractor'+currentItem+'"type="text" /></td></tr>';
$('#data').append(strToAdd);
});
});
//]]>
</script>
</head>
<body>
<div class="content">
<h2>header text</h2>
<p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
<form id="myform" name="form1" method="post" action="signup2.php">
<table class="dd" width="100px" id="data">
<tr>
<td>Area:<input class="textfield" name="area0" id="area0" type="text" /></td>
<td>Contractor:<input class="textfield" name="contractor0" id="contractor0" type="text" /></td>
</tr>
</table>
<input class="subbutton" type="submit" name="Submit" value="Submit Form">
</form>
<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" />
<br>
</div>
</body>
</html>
PHP:
<?php
for( $i = 0; $i <= $count; $i++ )
{
date_default_timezone_set('America/New_York');
$today = date("m.d.y");
$area0 = $_POST['area'.$i];
//the data
$data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";
//open the file and choose the mode
$fh = fopen("users2.csv", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
}
?>
Well, if this is all of the code and you didn't leave anything out I'd say you need to initialize $count.
Also, I'd probably open the file handle before the loop and close it after the loop instead of needlessly opening and closing it.
Edit - adding code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="mdcstyle.css" />
<title>Submission form</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
var currentItem = 0;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td>Area:<input class="textfield" name="area['+currentItem+']" id =" area['+currentItem+']" type="text" /></td><td>Contractor:<input class="textfield" name="contractor['+currentItem+']" id ="contractor['+currentItem+']"type="text" /></td></tr>';
$('#data').append(strToAdd);
});
});
//]]>
</script>
</head>
<body>
<div class="content">
<h2>header text</h2>
<p>Please complete this form for Udpates. Add a new line if you have more than one project.</p>
<form id="myform" name="form1" method="get" action="signup2.php">
<table class="dd" width="100px" id="data">
<tr>
<td>Area:<input class="textfield" name="area[0]" id="area[0]" type="text" /></td>
<td>Contractor:<input class="textfield" name="contractor[0]" id="contractor[0]" type="text" /></td>
</tr>
</table>
<input class="subbutton" type="submit" name="Submit" value="Submit Form">
</form>
<button id="addnew" name="addnew" value="Add new item">Add new entry</button>
<input type="hidden" id="items" name="items" value="1" />
<br>
</div>
</body>
</html>
php:
<?php
$area = $_GET['area'];
$count = count($area);
//open the file and choose the mode
$fh = fopen("users2.csv", "a");
for( $i = 0; $i <= $count; $i++ )
{
date_default_timezone_set('America/New_York');
$today = date("m.d.y");
$area0 = $area[$i];
//the data
$data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";
fwrite($fh, $data);
}
fclose($fh);
?>
I just made the minimal edits, you aren't using anything but the area variable from your form, and there are a lot of values you have listed that aren't initialized anywhere (hours, project, etc). Also, I haven't got access to anyplace to do actual php code right now, and this is from memory, so there may be typos/etc.
Related
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
I have a billing page which fetches data from the cart.
I am having a problem submitting these values to the database. How do i correct my php to get it to submit to the database correctly?
For example, i have a transactions table in my database, and i want all payment details to go there, which can later be fetched and outputted in the admin section as a receipt of order.
I cannot get the name, address, email, phone to submit correctly. It comes up with 0 in the database, how do i fix this?
How can i specifically use AJAX when the button is clicked to show a loading bar/page and then the success message?
I also get the error:
Notice: Undefined index: command in F:\xamppnew\htdocs\web\billing.php on line 5
Code:
<?php
include "storescripts/connect_to_mysql.php";
session_start();
if($_REQUEST['command']=='update'){
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$address=$_REQUEST['address'];
$phone=$_REQUEST['phone'];
$item_id = $_SESSION['item_id'];
$quantityorder = $each_item['quantity'] = $_SESSION['quantity1'];
$cartTotal = $_SESSION['cartTotal'];
// Add this product into the database now
$sql = mysqli_query($link,"insert into transactions values('','$item_id','$quantityorder','$cartTotal','$name','$address','$email','$phone')");
die('Thank You! your order has been placed!');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
<title>Billing Info</title>
<script language="javascript">
function validate(){
var f=document.form1;
if(f.name.value==''){
alert('Your name is required');
f.name.focus();
return false;
}
f.command.value='update';
f.submit();
}
</script>
</head>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<div id="pageContent">
<div style="margin:24px; text-align:left;">
<br />
<form name="form1" onsubmit="return validate()">
<input type="hidden" name="command" />
<div align="center">
<h1 align="center">Billing Info</h1>
<table border="0" cellpadding="2px">
<tr><td>Product ID:</td><td><?php echo $item_id = $_SESSION['item_id'];?></td></tr>
<tr><td>Total Quantity:</td><td><?php echo $each_item['quantity'] = $_SESSION['quantity1']; ?></td></tr>
<tr><td>Order Total:</td><td>£<?php echo $cartTotal = $_SESSION['cartTotal'];?></td></tr>
<tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td> </td><td><input type="submit" value="Place Order" /></td></tr>
</table>
</div>
</div>
<?php include_once("template_footer.php");?>
</form>
</body>
</html>
did you use Mysqli to connect ?
if yes, change mysql_query to mysqli_query
and you need to add 'name' attribute to the submit button
and do this
if(isset($_POST['submit'])){....
It is a good practice to use addslashes over your values before concat them in an sql query
$name = addslashes($_REQUEST['name']);
$email = addslashes($_REQUEST['email']);
$address = addslashes($_REQUEST['address']);
$phone = addslashes($_REQUEST['phone']);
I have read a few posts from on here and a few from w3schools, but i don't seem to be able to get my head around it.
essentially i have a page that needs to load a list of users that have logged into a system between 2 dates. their boss will log in and using his/her group id, dateto and datefrom, generate a drop down list that displays all the users that logged in during that period.
so far i have managed to be able to pass 1 selection value across to the php called by the ajax, but i can't manage to work out how to "post" multiple input selections.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Operator Portal</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style type="text/css">
.smallfont
{
font-size: 9px;
}
</style>
<script>
$(function() {
$('.datepicker').datepicker({ minDate: -21, maxDate: "+1D" });
});
</script>
</head>
<body>
<form action="results.php" method="post" target="_blank" class="ui-dialog-content" title="Owner Operator Web Portal">
<?php
include 'joomla-auth.php';
$name = JFactory::getUser() ->username;
?>
<input type="hidden" name="operatorID" value="<?php echo $name; ?>">
<p><strong>To show job details select the Car Number and Date Range below :</strong></p>
<table width="374" border="0" cellpadding="1">
<tr>
<td width="93"><strong>Car Number:</strong></td>
<td width="271"><select name="CarNumber" size="1" id="CarNumber" type="text">
</td>
</tr>
<tr>
<td><strong>Date From: </strong></td>
<td><input type="text" class="datepicker" name="DateFrom" id="DateFrom" />
<span class="smallfont">(date format: mm/dd/yyyy)</span></td>
</tr>
<tr>
<td><strong>Date To:</strong></td>
<td>
<input type="text" class="datepicker" name="DateTo" id="DateTo" />
<span class="smallfont">(date format: mm/dd/yyyy)</span></td>
</tr>
<tr>
<td height="50"> </td>
<td><table width="100%" border="0" cellspacing="0" cellpadding="4">
</table>
<input name="Submit" type="submit" class="ui-buttonset" id="Submit" formaction="results.php" formmethod="POST" title="Submit" value="Submit">
<input name="Reset" type="reset" class="ui-buttonset" id="Reset" title="Clear All" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
What i would like to be able to do is add another form element which is populated by the aforementioned ajax call
i think its meant to look something like this, but i cant manage to get it to work.
<html>
<head>
<script>
function showUser() {
// Retrieve values from the selects
var u = document.getElementByID('DateFrom').value;
var g = document.getElementByID('DateTo').value;
if (u=="" || g=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?u="+u+"&g="+g,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<div id="u">
<input type="text" class="datepicker" name="DateFrom" id="DateFrom" />
<span class="smallfont">(date format: mm/dd/yyyy)</span></td>
</div>
<div id="g">
<input type="text" class="datepicker" name="DateTo" id="DateTo" />
<span class="smallfont">(date format: mm/dd/yyyy)</span></td>
</div>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
Im obviously missing something very obvious. Now i realise that i havent put the hidden "input" into this second section of code, i was working on getting 2 inputs working before trying to get a 3rd.
thanks,
Michael
looks like you mispelled some javascript
var u = document.getElementByID('DateFrom').value;
var g = document.getElementByID('DateTo').value;
the 'ID' should be a 'Id.' Lower case D. Try that - it should be the reason why nothing is getting passed
var u = document.getElementById('DateFrom').value;
var g = document.getElementById('DateTo').value;
Also check that the DOM element is built and exists when the function is called. Putting the JS at the end of the page helps.
Another Thing to help understand AJAX is that the controller (getuser.php) receives its variable via $_GET, as you have specified. So when clicking on your button, you call your AJAX function and shoot it your variables (u & g) and your controller handles them via $_GET.
Your controller spits out an output and that is what received back on your original page. So you can simply echo a string with as many variable as you like in a format you can parse correctly. for example:
you echo an output from getuser.php:
echo $var1.'|'.$var2.'|'.$var3;
Then your original page can process further or w.e you need:
data = xmlhttp.responseText.split ( "|" );
var1 = data[0];
So you dont need any traditional HTML form when using AJAX. Simply code your variables with your Javascript function. For example:
<div id="fancyButton" onclick="sampleAJAXfunction(var1,var2,var3)"></div>
I hope this helps with your original question.
i make a dynamic textbox through javascript now i want to post the data of all dynamically generated textboxes to php script and then insert into a table.
how can i do this...
<head>
<title>Dynamic Form</title>
<script language="javascript">
function changeIt()
{
var i = 1;
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>"
}
</script>
</head>
<body>
<form name="form" action="post" method="">
<input type="button" value="add another textbox" onClick="changeIt()">
<div id="my_div">
<input type="submit" value="save">
</body>
HI, whenever you post your form at that time all the fields are posted on the php script, where you can get array of that textbox variable by using for loop.
ex:
$textboxarr = array();
for($i = 0;$i<count($_POST['mytext']);$i++){
$textboxarr = $_POST['mytext'][$i];
}
now you having your all text boxes in the $textboxarr.try this.
Thanks.
Use it like this
<head>
<title>Dynamic Form</title>
<script language="javascript">
function changeIt()
{
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext[]'>"
}
</script>
</head>
<body>
<form name="form" action="post" method="">
<input type="button" value="add another textbox" onClick="changeIt()">
<div id="my_div">
<input type="submit" value="save">
</body>
In php you will be able to use it as an array like
$_POST['mytext'][0], $_POST['mytext'][1] etc... or you can use a foreach loop for $_POST['mytext']