Invoke Combobox from PHP in HTML - php

I have a PHP file that parses a JSON list and feeds it to a combobox:
<?php
$jsonData = '{"marco":"marco#test.it", "giovanni":"giovanni#mail.it"}';
$json = json_decode($jsonData, true);
$opts = '';
foreach($json as $name => $email)
{
$opts .= '<option value="'.$email.'">'.$name.'</option>';
}
echo ' <select name="Team1">'.$opts.'</select> <br> ';
echo ' <select name="Team2">'.$opts.'</select> <br>';
?>
I'm trying to include it in an HTML page, so that when it loads it will show the combobox:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<? include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>
But I can't manage to make them appear!
I don't need a real time update, it's enough that the list is updated when the page is created.
Also, I can't manage to read the associated value from the combobox.

Make sure that the HTML page has the .php extension instead of .html and check if your webserver supports short open tag for php.
Edit:
You can check the phpinfo() if short_open_tag has the value on.
Edit 2:
You can access the selected value in your mailer.php with $_POST['Team1'] and $_POST['Team2']

Edit:
<? include("combobox.php") ?>
to
<?php include("combobox.php") ?>
So, html code will be:
<html>
<head>
<title> Invio Mail </title>
</head>
<body>
<form name="mail" action="mailer.php" method="post">
<p>
<center><b> Invio mail </b>
<br>
<br>
<?php include("combobox.php") ?><br>
Messaggio: <input type="text" name="name" rows="5" required><br>
<br>
<center><input type="submit" value=" Invia "></center><br>
</form>
</body>
</html>

Related

I am trying to use an HTML form to append the results to a Text file using php

Here is my HTML form
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="example.php" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" /> <input type="Reset”/></p>
</form>
</p>
</body>
</html>
PHP file "example.php" Shows the Client what was inputted and should append to the text file "phpfile.txt"
<html>
<body>
<p>
Hi <?php echo htmlspecialchars($_POST['fname']); ?>.<br>
<?php echo htmlspecialchars($_POST['lname']); ?>
</p>
<?php
if(isset($_POST['Submit'])){
$fname = $_POST['fname']."
";
$lname = $_POST['lname']."
";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname);
fwrite($file, $lname);
fclose($file);
}
?>
</body>
</html>
I am getting the php file to print for the user but it is not appending to the .txt file. I am unsure where I am going wrong
You are probably getting a syntax error.
You also need to use \n to add a new line. You also need to place the echo $_POST parts in the isset function.
<html>
<body>
<h1>HTMl FORM</h1>
<p>
<form action="" method="POST">
<p>Your First name: <input type="text" name="fname" /></p>
<p>Your Last name : <input type="text" name="lname" /></p>
<p><input type="Submit" name="Submit" value="Submit" />
<input type="Reset"/>
</p>
</form>
</p>
<?php
if(isset($_POST['Submit'])){
echo 'Hi '.htmlspecialchars($_POST['fname']);
echo '<br>';
echo htmlspecialchars($_POST['lname']);
$fname = $_POST['fname']."";
$lname = $_POST['lname']."";
$file=fopen("phpfile.txt", "a");
fwrite($file, $fname." ".$lname. "\n");
fclose($file);
}
?>
</p>
</body>
</html>

PHP Form Post/Redirect/Get Header Location Error

I have a simple form that I've made to help me learn php. I'm using my local host server to host the php file (form.php). However, when I refresh the page it resubmits the form, I know I can use the post/redirect/get method to negate this problem. Except implementing the header('Location: form.php'); hasn't been working, if you could take a look at the code- and tell me what I'm doing wrong, that would be much appreciated.
Code example i.e without header('Location: form.php');
<?php
if (empty($_POST) === false) {
echo '<pre>', print_r($_POST, true), '</pre>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
This is the result...
Screen Shot without header location
then I add:
header('Location: form.php');
And I get this...
To prevent a form from resubmitting on page refresh, two methods are used:
Method 1: Use AJAX + Redirect
Submit your form using AJAX and then redirect to another page using JQuery.
Method 2: Reload the page
Refresh the page using Javascript.
Your code should be like this:
<?php
if (!empty($_POST)) {
echo '<pre>', print_r($_POST, true), '</pre>';
echo '<script type="text/javascript"> location.reload();</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form action="form.php" method="post">
<p>
<label for="name">Name:</label><br>
<input type="text" name="name" id="name"><br>
</p>
<p>
<label for="email">Email:</label><br>
<input type="text" name="email" id="email"><br>
</p>
<p>
<label for="message">Message:</label><br>
<textarea name="message" id="message"></textarea>
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
</body>
</html>

Basic Form using HTML and Php

I am trying to create just a basic form using Php and HTML. When I type into the fields and click submit nothing appears on the page except for "Hi,"
This is what I have for the HTML document
<!DOCTYPE html>
<html>
<head>
<title>Php Form</title>
</head>
<body>
<form method="post" action="PHPD1.php">
<label> First Name:<input name="FName" type="text"/></label>
<br>
<label> Last Name:<input name="LName" type="text"/></label>
<br>
<label>Message: <textarea name="message">
</textarea>
</label>
<br>
<input name="submit" type="submit" value="Submit"/>
</form>
</body>
</html>`
and for the php document I have
<html>
<head>
<title>Deliverable 1</title>
</head>
<?php
// Capture the values posted to this php program from the text fields
$FName = $_POST['FName'] ;
$LName = $_POST['LName'] ;
$Message = $_POST['Message'] ;
?>
<body>
Hi, <?php print $FName; ?>
<br>
<?php print $Message; ?>
</body>
</html>
<textarea name="message">
</textarea>
the name given for textarea is "message". so the same name should be used in the php page also to access the value.
instead of
$Message = $_POST['Message'] ;
use
$Message = $_POST['message'] ;
you have done a small mistake.
just check now.

Finding a page created by a php function

I am trying to generate an HTML page using data gathered in a form on another page. I have this tag on the html page with the form:
<html>
<body>
<meta http-equiv="cache-control" content="private" >
<link rel="stylesheet" type="type/css" href="vytran_css.css" />
<head> New Product Introduction </head>
<p> In order to begin the process of introducing a new product, please complete
the following form. Once you are satisfied with your responses to the various
prompts, please click on the submit button at the bottom of the page. If you
would like to start over, click the Reset button. If you have any questions,
Please follow the link that says "Help".
<form action="html_data.php" id=from1 method="post">
Product Name:
<input name="Name" size="20" type="text">
<br><br>
Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>
Team-members: <br>
<textarea name="Team-members" rows=10 cols=40 type="text"> </textarea> <br><br>
Product Type: <br>
<input name="Product Type" size="20" type="text"> <br><br>
Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>
<br> <br>
<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>
</p>
</body>
</html>
and on the html_data.php page I have the following:
<?php
ob_start(); // start trapping output
$name = #$_POST['Name'];
?>
<html>
<body>
<p>
Product Name: <?php echo $Name; ?><br>
Project Lead: <?php echo $PLname; ?><br>
Team Members: <?php echo $Team-members; ?><br>
Description: <?php echo $Description; ?>
</p>
</body>
</html>
<?php
$output = ob_get_contents();
$newfile="output.txt";
$file = fopen ($newfile, "w");
fwrite($file, $output);
fclose ($file);
ob_end_clean();
?>
This should do what I am asking it to do. The form submits with no problem, but I cannot find the page once it submits. Any idea what I need to change?
In your form
Change from
<textarea name="Team-members" rows=10 cols=40 type="text">
to
<textarea name="Team_members" rows=10 cols=40 type="text">
In your html_data.php
Change from
<?php echo $Team-members; ?>
to
<?php echo $Team_members; ?>
There is obviously an issue with the dash between Team and members.
PHP is treating the hyphen as a mathematical operation, being a minus.
I.e.: Team minus members
Then, if you wish to echo the data to screen after submission, add
echo $output;
underneath ob_end_clean();
This worked for me.

Printing text box value on page in PHP

I am new to PHP (in-fact I am learning it). I am trying to get the value of textbox defined in HTML and print it on HTML page through PHP code. I am able to send the value of textbox to another page using form post and getting it using $_POST['name'] on other page.
I am not able to print the value of textbox on same page.
Here is my code:
<html>
<head>
<?php
function myfunction()
{
printf($_POST['fname']);
}
?>
</head>
<body>
Name: <input type="text" name="fname" />
<input type="button" onClick="myfunction()" value="Click" />
</body>
</html>
First of all it's not required to write php function within <head> tags. Second,
you can't POST data without <form> tags. try this
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
$name=$_POST['fname'];
echo $name;
}
else {
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php } ?>
you can not call PHP function by button's click event...
if you want to print on same page then you can do by below code..
<html>
<?php
if(count($_POST)>0){
echo $_POST['fname'];
}
?>
<body>
<form method='post'>
Name: <input type="text" name="fname" />
<input type="submit" value="Click" />
</form>
</body>
</html>
hope you will get what you want by my answer..

Categories