Basically this is what I have:
<form method="POST" action="model.php">
<input type="hidden" name="from" value="edit">
<?php
include 'model.php';
$contactID = $_POST['editConRad'];
selectSingle($contactID);
echo "<input type='hidden' name='contactID' value='$contactID'>";
echo "<label>First Name:</label>";
echo "<input type="text" name="fname">";
echo "<label>Last Name:</label>";
echo "<input type="text" name="lname">";
echo "<label>Email:</label>";
echo "<input type="email" name="email">";
echo "<label>Street Address:</label>";
echo "<input type="text" name="streetaddress">";
echo "<label>City:</label>";
echo "<input type="text" name="city">";
echo "<label>Province:</label>";
echo "<input type="text" name="province">";
echo "<label>Postal Code:</label>";
echo "<input type="text" name="postalcode">";
echo "<label>Phone:</label>";
echo "<input type="number" name="phone">";
echo "<label>Date of Birth:</label>";
echo "<input type="date" name="yob">";
echo "<input type="submit" value="Submit Edit">";
?>
</form>
And I think there must be a better way to output html instead of having to output each line with echo, I thought there was a print function to do this but I couldnt find one.
Anyone have a better way of doing this? I know I could do one massive echo but there must still be something better.
I guess this way is better than what I have above but still not looking for a better way.
echo '<label>First Name:</label>.
<input type="text" name="fname">.
<label>Last Name:</label>.
<input type="text" name="lname">.
<label>Email:</label>.
<input type="email" name="email">.
<label>Street Address:</label>.
<input type="text" name="streetaddress">.
<label>City:</label>.
<input type="text" name="city">.
<label>Province:</label>.
<input type="text" name="province">.
<label>Postal Code:</label>.
<input type="text" name="postalcode">.
<label>Phone:</label>.
<input type="number" name="phone">.
<label>Date of Birth:</label>.
<input type="date" name="yob">.
<input type="submit" value="Submit Edit">';
You can switch between PHP and HTML whenever you wish just by adding the appropriate tags.
For example:
<?php
echo "This is output from an echo statement<br>"
?>
This is plain old HTML<br>
<?php
echo "And now we're back to PHP<br>"
Your form becomes:
<form method="POST" action="model.php">
<input type="hidden" name="from" value="edit">
<?php
// Do this bit in PHP
include 'model.php';
$contactID = $_POST['editConRad'];
selectSingle($contactID);
?>
<!-- Now switch back to HTML -->
<!-- PHP variable embedded here-------------|................| -->
<input type='hidden' name='contactID' value='<?= $contactID?>'>";
<label>First Name:</label>
<input type="text" name="fname">
<label>Last Name:</label>";
<input type="text" name="lname">
<label>Email:</label>
<input type="email" name="email">
<!-- and so on -->
</form>
<?php
// You can store long text strings in HEREDOC or NOWDOC syntax
$longString = <<<'EOT'
This is an arbitrarily long string, but this one is only short<br>
EOT
echo $longString;
See the PHP manual for all the details on how PHP handles strings, HEREDOC, NOWDOC, and the significance of single and double quotes.
Related
I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>
Part of code:
echo "<form method ='post' action='NextFile.cgi'>";
echo "<table>";
echo "<tr><td>Date: </td></td> <input type='date' name='Date' value =".$record['Date']." autofocus required='required'
/></td></tr>";
echo "<tr><td>Time: </td></td> <input type='time' name='Time' value=".$record['Time']." autofocus required='required'
/></td></tr>";
echo "</table>";
echo "<input type='submit' name='submitname' value='Save' />";
echo "</form>";
$record is related to the mysql query.
When running the code in a web browser, the two input boxes are displayed side by side.
Below are the characters />
Below is 'Date:'
Below is 'Time:'
I am trying to alter the code so that Date: is beside the Date input box, and the same for Time below.
How can I do it?
The main problem is your </td></td> which should be </td><td>
$date = $record["Date"];
$time = $record["Time"];
<form method ="post" action="NextFile.cgi">
<table>
<tr><td>Date: </td><td><input type="date" name="Date"$date" autofocus required="required"/></td></tr>
<tr><td>Time: </td><td><input type="time" name="Time"$time" autofocus required="required"/></td></tr>
</table>"
<input type="submit" name="submitname" value="Save" />
</form>
Here's a cleaned up version of your code:
echo '<table>';
echo '<tr><td>Date: </td> <input type="date" name="Date" "value="'.$record['Date'].'" autofocus required="required" /></td></tr>';
echo '<tr><td>Time: </td> <input type="time" name="Time" value="'.$record['Time'].'" autofocus required="required"/></td></tr>';
echo '</table>';
As an explanation, I added the quotes around your values (not sure if that was causing your issue, but it's good form; and your results might have erred without it) and removed the second close row </td> as was mentioned above.
Edit: try it now
I have a page with mixed sources of content. The top half of it is hard coded html with values echo'd from a db like so:
Part Number: <input type="text" name="pn" size="25" id="1" value="<?php
echo "$STH->pn"; ?>"/>
Part Nomenclature: <input type="text" id="partnomen" name="part_nomenclature"
size="35" value="<?php echo "$STH->part_nomenclature"; ?>"/>
Quantity:<input type="text" name="qty" size="3" value="<?php echo "$STH->qty";
?>"/><br />
Serial Number <input type="text" name="sn" size="25" value="<?php echo "$STH->sn";
?>"/>
ATA Code: <input type="text" name="ata" size="12" value="<?php echo "$STH->ata"; ?>"/>
Control Order: xxxxxx
Engine Model: xxxxxxxx<br />
Engine Serial Number: xxxxxxxx
Removed From A/C Serial# <input type="text" name="acsn" value="<?php echo
"$STH->acsn"; ?>"/>
TT / TC: ___________________-->
<input type="hidden" name="db_date" value="<?php echo "$STH->db_date"; ?>"/>
<h3 id="addinfo">Reason For Workorder: </h3>
The middle contains html and values that are both echo'd onto the page:
echo "
<div id='addlinfo'>
<input type='hidden' name='wo_id' value='$wo_id'/>
Sequence<input type='text' name='sel_id[]' size='1' value='$i'/>
Repair ID:<input type='text' name='repair_id[]' size='1'
value='$STH->repair_id'readonly/>
Part Nomenclature: <input type='text' name='' size='35'
value='$STH->part_nomenclature'readonly/>
Repair Name:<input type='text' name='repair_name[]' size='20'
value='$STH->repair_name' readonly/><br />
Location: <input type='text' name='location[]' size='20' value='$STH->location'
readonly/>
Description:<br /> <textarea id='' rows='5' cols='100' name='description[]'
id='text'>$STH->description</textarea>
</div>";
The bottom half is pulled in from a separate php page using a jquery ajax call like so:
$(document).ready(function(){
$('#button1').click(function(){
$.ajax({
url: 'http://localhost/php_site_copy/process.php',
type: 'POST',
data: {part_name: $('#partnomen').val()},
success: function(msg){
$('#result').html(msg);
}
}); //event handler
});
});
<div id="result"></div><br />
<button id="button1">Add Repairs</button>
The goal here is to collect all of the data displayed into a $_POST from the page but I'm finding that I only get part of the array. The bottom part form elements are not picked up in the post at all and only some of the upper form elements.
You would think that this would work but I'm beginning to think I'm traveling down the wrong road when it comes to mixing content from different sources. Is there a standard that I'm missing or should I just put my nose back to the grindstone?
Edit: to answer quids' question, I'm using Firefox and the form arrangement is like so:
<form action='test_page.php' method='post'/>
<top html inputs/>
<middle inputs/>
<bottom inputs/>
<input type='submit' value='submit'/>
</form>
<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}
I am trying to access the style attribute of a element in order to display a form however, I get this error: Uncaught TypeError: Cannot read property 'style' of undefined. The example below is a simplified example.
cookies.php
echo "[a href='test.php?flavor=butter']Bake Butter Cookies[/a]";
echo "[a href='test.php?flavor=chocolate'] Bake Chocolate Cookies [/a]";
?>
test.php
switch($cookieType){
case "chocolate":
echo "<script type='text/javascript'>";
echo "document.chocolate.style.display='block'; ";
echo "</script>";
break;
case "oatmeal-raisin":
echo "<script type='text/javascript'>";
echo "document.oatmealraisin.style.display='block'; ";
echo "</script>";
break;
case "butter":
echo "<script type='text/javascript'>";
echo "document.butter.style.display='block'; ";
echo "</script>";
break;
}
?>
<div id="chocolate" style="display:none;">
<form action="<?php echo $_SERVER['php_self']?>">
<input type="hidden" name="type" value="chocolate"/>
What is your name: <input type="text" name="your_name"/>
How many chocolate chips on each cookie? <input type="text" name="chips"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
</div>
<div id="oatmealraisin" style="display:none;">
<input type="hidden" name="type" value="oatmealraisin"/>
<form action="<?php echo $_SERVER['php_self']?>">
What is your name: <input type="text" name="your_name"/>
How many raisins on each cookie? <input type="text" name="raisins"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
</div>
<div id="butter" style="display:none;">
<form action="<?php echo $_SERVER['php_self']?>">
<input type="hidden" name="type" value="butter"/>
What is your name: <input type="text" name="your_name"/>
How many many cookies? <input type="text" name="cookies"/>
<input type="submit" name="BAKE!"/>
<input type="reset"/>
</form>
I have never had problems accessing the style attribute of a element before so I am not sure what is the problem. The switch statement does work because the javascript console shows the output of the right case. I need another pair of eyes. Any suggestions?
It's not the style attribute that is the problem, you failed to access the elements themselves.
You are trying to access the elements as if they are global variables in the document object, that is only true in IE in quirks mode. You need to use the getElementById method. Replace:
document.chocolate
with:
document.getElementById('chocolate')
and corresponding for each element.