connection between php scripts and html input - php

<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" " />
</form>
I want to make that value in php script ... be related to the value of the input form html
I tried so many ways ,but not implemented , please help

Try it like
PHP in HTML :
<input type="text" class="span3" name="dateField1" value="<?php echo $dateField1->makeDateField();?>" />
Or even you can try like
HTML in PHP :
<?php echo '<input type="text" class="span3" name="dateField1" value="'. $dateField1->makeDateField().'" />';?>

Try this:
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
$value = $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value=" <?php echo $value; ?> " />
</form>

You can use php inside html by starting php tags within html
<form id="submit-form" method="post" action="" enctype="multipart/form-data">
<label>dateField1</label>
<?php
print "<b>calendar:</b><br/>";
$dateField1 = new dateField($format,"date1",$img);
$dateField1->setTitles($arr_daysOfTheWeek,$arr_months,$format_title);
$dateField1->setCssClasses($arr_cssClasses);
print "value:" . $dateField1->makeDateField();
?>
<input type="text" class="span3" name="dateField1" tabindex="2" value="<?php echo $value;?>" />
</form>

Related

How to add Html code inside php

I am trying to create a button, which downloads a file, and this file is created based on php variables.
The "instruction" to show this html code is inside a php file.
So far, I've got this ( donĀ“t think it's ok ):
Inside the php this generates an email:
$addressforics = get_valueFromStringUrl($_SERVER['HTTP_REFERER'], 'address');
ob_start();
<form method="post" action="/download-ics.php">
<input type="hidden" name="date_start" value="2017-1-16 9:00AM">
<input type="hidden" name="date_end" value="2017-1-16 10:00AM">
<input type="hidden" name="location" value=$addressforics>
<input type="hidden" name="description" value="This is my description">
<input type="hidden" name="summary" value="This is my summary">
<input type="hidden" name="url" value="http://example.com">
<input type="submit" value="Add to Calendar">
</form>
$my_var = ob_get_clean();
How to insert the php variable $addressforics into the HTML code ( which is inside the same php code, where $addressforics is defined )?
You have to write the php code between <?php and ?>, as documented here.
Something like..
<?php
$addressforics = get_valueFromStringUrl($_SERVER['HTTP_REFERER'], 'address');
ob_start();
?>
<form method="post" action="/download-ics.php">
<input type="hidden" name="date_start" value="2017-1-16 9:00AM">
<input type="hidden" name="date_end" value="2017-1-16 10:00AM">
<input type="hidden" name="location" value="<?php echo $addressforics; ?>">
<input type="hidden" name="description" value="This is my description">
<input type="hidden" name="summary" value="This is my summary">
<input type="hidden" name="url" value="http://example.com">
<input type="submit" value="Add to Calendar">
</form>
<?php
$my_var = ob_get_clean();
?>
to write php value into html element
<input name="test" value="<?php echo $a ; ?> ">
or
<?php echo "<input name='test' value='".$a."'>"; ?>

Submit form value return

I have this source code...
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
In the following code I get the values for 'note'...
if (isset($_POST["submit_eingabemaskeR"]))
{
echo ("<br/>");
//Note
echo $_POST["note"];
But how can I echo the value of the first field -> SpielerName?
SpielerName is not a form field, its just text.
If you want its data submitted you can make a hidden form field with that value.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo htmlspecialchars($SpielerName); ?>
<input type="hidden name="SpielerName" value="<?php echo htmlspecialchars($SpielerName); ?>">
<br>Note: <input type="text" name="note" value=<?php echo htmlspecialchars($Note) ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo htmlspecialchars($Tore) ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">
Use a hidden input.
<form method="post" id="center" action="">
<br>SpielerName: <?php echo $SpielerName; ?>
<input type="hidden" name="SpielerName" value=<?php echo $SpielerName; ?> >
<br>Note: <input type="text" name="note" value=<?php echo $Note ?> >
<br>Tore: <input type="text" name="tore" value=<?php echo $Tore ?> >
<br><br><input type="submit" name="submit_eingabemaskeR" value="Abschicken">

populate input field with PHP variable

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>

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help

TwoPHP blocks in file

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>
Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>
You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

Categories