Form data not posting to uri - php

Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>

Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.

Related

Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?
P.S The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method.
My code is as follow
<?php
include_once 'header.php';
$id = mysqli_real_escape_string($conn, $_GET['bookid']);
$title = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="signup-form">
<h2>Pre-order</h2>
<div class="signup-form-form">
<form action="preorder.inc.php" method="post">
<input type="text" disabled="disabled" name="id" value= $id >
<input type="text" name="uid" placeholder="Username...">
<input type="text" name="BRO" placeholder="BookRegistrationCode...">
<button type="submit" name="submit">Pre-order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "wronginfo") {
echo "<p>Wrong information!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Success!</p>";
}
}
?>
</section>
Put <?php echo $var; ?> inside the value attribute.
<input type="text" value="<?php echo $var; ?>" />
EDIT
As per #arkascha's comment, you can use alternative php short tags:
<input type="text" value="<?= $var; ?>" />
As per the docs: https://www.php.net/manual/en/language.basic-syntax.phptags.php

Php and HTML forms - random values in input fields

I write a conversion from celsius to fahrenheit and vice versa. The problem is that in the form fields now random values ​​are displayed to me. How to convert this code so that after entering the value, the converted value in the second field appears in the first field?
Is it possible to use only php at all?
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
I want the value entered in the first field to be shown in the second transformed.
You can do all php if you post back to itself. If the page with the input is calc.php
Add an else to set the value to empty string.
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
} else {
$cel = '';
}
if(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
} else {
$fah = '';
}
Try something like this
$cel="";
$fah="";
if(isset($_POST['fah']) && !empty($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}
if(isset($_POST['cel']) && !empty($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
You can try this example:
$celValue = $_POST['cel'];
$fahValue = $_POST['fah'];
if( ! empty($fahValue)){
$celValue = fahrenheitToCelcius($_POST['fah']);
}
if( ! empty($celValue)){
$fahValue = celciusToFahrenheit($_POST['cel']);
}
function celciusToFahrenheit($cel) {
return ($cel - 32) * 1.8;
}
function fahrenheitToCelcius($fah) {
return ($fah + 32) / 1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $celValue; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $fahValue; ?>" name="cel">
<input type="submit" value="Calc">
</form>
Since both the variables give isset() true so we can have something like this
if(isset($_POST['fah']) && isset($_POST['cel'])) {
//if cel is not empty
if(!empty($_POST['cel'])) {
$cel = $_POST['cel'];
$fah=($cel-32)*1.8;
} else if(!empty($_POST['fah']){
$fah = $_POST['fah'];
$cel = ($fah+32)/1.8;
}
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>
You just missing set default values that will overwrite if other post value exist, and else if to load only one parameter so other will be empty
<?php
$cel = "";
$fah = "";
if(isset($_POST['fah'])){
$cel=($_POST['fah']+32)/1.8;
}elseif(isset($_POST['cel'])){
$fah=($_POST['cel']-32)*1.8;
}
?>
<html>
<body>
<form method="post" action="calc.php">
Fahrenheit: <input id="inFah" type="text" placeholder="Fahrenheit" value="<?php echo $fah; ?>" name="fah">
Celcius: <input id="inCel" type="text" placeholder="Celcius" value="<?php echo $cel; ?>" name="cel">
<input type="submit" value="Calc">
</form>

PHP inside of HTML inside of PHP

How would I go about doing this?
<?php
if (isset ($_SESSION['ID'])) {
echo "
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '" . echo $_SESSION['ID'] . "' placeholder = 'Email' size = '30' required/>
</form>
?>
I'm trying to pull a var from the session and put it inside a form value and can't figure out how to do so.
It's not recommended to echo your whole html in PHP... You could do it like this:
<?php if(isset($_SESSION['ID'])): ?>
<form action='updateacct.php' method='POST'>
Email: <input type='text' name='eml' value='<?php echo $_SESSION['id']; ?>' placeholder='Email' size='30' required/>
</form>
<?php endif; ?>
No need for the second echo. You are already echoing.
I took your code and simplified it a bit. I use multiple echos to make it clearer what we do.
<?php
if (isset($_SESSION['ID'])) {
echo '<form action="updateacct.php" method="POST">';
echo ' Email:';
echo ' <input type="text" name="eml" value="' . $_SESSION['ID'] . '" placeholder="Email" size="30" required />';
echo '</form>';
}
?>
I would go like this:
<?php if (isset ($_SESSION['ID'])) : ?>
<form action = 'updateacct.php' method = 'POST'>
Email:
<input type = 'text' name = 'eml' value = '<?= $_SESSION['ID'] ?>' placeholder = 'Email' size = '30' required/>
</form>
<?php endif; ?>
You can say:
<?php
if (isset ($_SESSION['ID'])) {
?>
// HTML goes here
<?php
}
?>

Table Generator

Hi I have a question about generating rows and columns. would like to ask about how can I make it in one page only. This is what I have tried.
HTML:
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="get_table/execute_table.php" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
PHP:
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Yes, it is fully running but I want it in 1 page only. Thanks.
Normally, if you want it on the same page, you just omit the action="" with its value.
Then of course, put the php process in the same page as the form:
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<!-- ^^ no more value, well you could just put the same filename -->
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
<?php
$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings
if(isset($_POST['submit'])) { // catch submission button
$row = $_POST['title1'];
$column = $_POST['title2'];
$out .= "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
$out .= "<tr>";
for($td=1;$td<=$column;$td++){
$out .= "<td>row: ".$tr." column: ".$td."</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
}
echo $out; // finally echo it
to achieve this by using only one page just leave the action attribute empty. and add your php block on top.and make sure to save it as .php and place your php code block on top of all html
so in the end it will look like this
<?php
$row = $_POST['title1'];
$column = $_POST['title2'];
echo "<table border='1'>";
for($tr=1;$tr<=$row;$tr++){
echo "<tr>";
for($td=1;$td<=$column;$td++){
echo "<td>row: ".$tr." column: ".$td."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
<html>
<head>
<title>Table Generator</title>
<body>
<center><h1>Generate Your Table</h1></center>
<div id="div1">
<center><h4>Enter number of Row and Column</h4>
<form action="" method="POST">
<label for="title">Row</label>
<input type="text" name="title1" placeholder="Row">
<br>
<label for="title">Column</label>
<input type="text" name="title2" placeholder="Column">
<br>
<input type="submit" name="submit" value="Generate Table"> </center>
</form>
</div>
</body>
You need to post to the same page, and check if the PhP Post array has data yet.
Replace the form action with:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
Note: although most (all?) browsers support self-posts with a blank action, this is not technically W3C compliant.
Then when the page is submitted it will reload the same page and populate the POST array. Somewhere on your page add a condition similar to:
if($_POST['title']){
//do whatever get_table/execute_table.php did
}else{
//echo the form here or, if you're allowed, use an include()
}
More info on self-posting:
How do I make a PHP form that submits to self?

how to display the result after the submit php form

how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>

Categories