Simple script issue? - html

for some reason I cannot get any form to work correctly on my website, I even went to w3school and copied a simple test form to see if it works, and it does not, here it is:
welcome.php:
<?
Welcome <?php echo $_GET["fname"]; ?>!<br />
You are <?php echo $_GET["age"]; ?> years old.
?>
form:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
I'm not sure if it matters or not, but I tried with and without brackets, and still I get a blank page, in explorer I get a 500 Error, most likely causes is maintenance or Programming Error", but I had the same issue last night so I doubt its maintenance, and everything else works.

Remove the beginning <? and ending ?> from your welcome.php.

Your form uses get and you're reading from $_POST.
Either change html to
<form action="welcome.php" method="post">
or change the php to
Welcome <?php echo $_GET["fname"]; ?>!<br />
You are <?php echo $_GET["age"]; ?> years old.
Also, make sure the value is present using the isset
Welcome <?php echo isset($_GET["fname"]) ? $_GET["fname"] : "Guest"; ?>!<br />

When you use $_POST["fname"] you should also specify post as method in your form
welcome.php:
<?
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
?>
form:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

Related

How to use value from input tag in the same form

I am looking how to reuse the value="typed_from_user" into another tag in the same formulary without using js, just PHP & HTML.
Is it even possible?
For example, here i want to reuse the word 'pizza'
<form>
<input name="hello" type="text" value="I want pizza">
<input name="order" type="text" value="pizza">
</form>
maybe something storing it in a variable?
<form>
<input name="hello" type="text" value="<?php echo $whatyouwant ;?>">
<input name="lunch" type="text" value="<?php echo substr($whatyouwant,7,5);?>"><!--cut it from letter 7, the word pizza-->
<input name="order" type="submit" value="submit">
</form>
yeah, supouse that we know where exactly starts the word pizza
This are the files i am using as tests
action.php
form.php
<?php
//action.php
$var = $_POST['hello'];
$var_lunch= $_POST['lunch'];
echo "hello: $var<br>";
echo "lunch: $var_lunch<br>";
?>
file form.php:
<html>
<header>
<title>test-page</title>
<!--bootstrap-->
<link rel="stylesheet" type="text/css" href="../../../css/bootstrap/bootstrap_v4.0.0.css">
</header>
<body>
<?php
$whatyouwant=777;
?>
<form method="post" action="action.php">
<input name="hello" type="text" value="<?php echo $whatyouwant ;?>">
<input name="lunch" type="text" value="<?php echo substr($whatyouwant,7,5);?>">
<input name="order" type="submit" value="submit">
</form>
</body>
</html>
OUTPUT:
hello: i want pizza
lunch:
If you are trying to get the input value form one text field to another text field only using PhP and HTML, I have to say that it is quite impossible. PhP is the server side language so to get the value from one field to another, it must first reach the server and then it display but will show error in PhP.
To achieve this, you have to use JS. You can achieve this from just a few lines of code using jQuery.

How do I remove the \"\" from a php echo?

When I enter quotes in a form and have that data showed in an echo it puts the quotes like this, \"Text Here\". How do I remove the \ from the quotes so it just says "Text Here"?
Code #1
<form action="index2.php" method="post">
Enter Your Name: <br>
<input type="text" name="name" placeholder="Enter Your Name Here"><br>
Enter Custom Quote: <br>
<input type="text" name="quote" placeholder="Exe; "I had a dream" "><br>
<input type="submit" value="Send">
</form>
Code #2
<html>
<head>
</head>
<body>
Thank you for sending us your quote <?php echo $_POST["name"]; ?>.
You quote was <?php echo $_POST["quote"]; ?>
</body>
If you have PHP Version <5.4.0, then your problem is probably related to magic quotes, which was deprecated in PHP 5.4.0.
Use the stripslashes function.
<body>
Thank you for sending us your quote <?php echo stripslashes($_POST["name"]); ?>.
Your quote was <?php echo stripslashes($_POST["quote"]); ?>
</body>
You should also use other functions like htmlspecialchars to decrease the likelihood of someone successfully hacking your code by injecting php code into the form.

Reset a form, without javascript? (input type=reset not working)

Well, I guess the title says it all.
I'm looking for a way to reset all the fields within a form.
I've tried some of the following:
<input type="reset" value="Clear all fields">
And
<button type="reset">Clear all fields</button>
Yet, none of this seems to be working.
This is a stripped version of my form.
<form id="form2" action="mainframe.php?paso=21" method="POST">
<input type="reset" value="Reset">
<button type="reset" form="form2">Reset</button>
<?php while($row=$resultado->fetch_assoc()){ ?>
<p>Number of <?php echo $row['name']; ?>
<input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
<?php } ?>
</form>
<button type="submit" form="form2">Send</button>
Edit: Apparently the reset button will replace the values of all inputs with the values they had on page load. The button would clear all fields and leave them blank only if the input's value property aren't declared or are null on page load.
Guess what. It actually DOES work. I didn't change anything at all. I promise:
<form id="form2" action="mainframe.php?paso=21" method="POST">
<input type="reset" value="Reset">
<button type="reset" form="form2">Reset</button>
<?php while($row=$resultado->fetch_assoc()){ ?>
<p>Number of <?php echo $row['name']; ?>
<input type="text" name="saldo[<?php echo $row['id']; ?>]" value="<?php echo $row['saldo']; ?>" maxlength="30" />
<?php } ?>
</form>
<button type="submit" form="form2">Send</button>
If it's not working on your site, then you may have another syntax error.
get rid of what u echo to the input value and it should work...
The previous answers are correct. The reset button resets the form's inputs to their initial values, not to blank.
In my case, I wanted to blank all of my inputs. I thought about writing some JavaScript that sets all the inputs to "", but I decided that just reloading the page would be easier. In my case, reloading the page blanks the form.
Try this:
<input type="button" onclick="window.location='http://www.yourwebsite.com/form.php'" value="Reset" />

Post HTML <form> on load

What is the best way to post a HTML Form on load? This is what I'm currently trying:
<?php
if ($Autopost == "1");
{
<body onLoad="mail.submit()">
<form method="POST" name="mail" class="adjacent" action="./Script/addmaillist.php">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<input type="hidden" name="genre" value="<?php echo $genre; ?>">
</form>
}
?>
I would just like to know if this is a good way, and if there is a better way?
I agree with Kolink that it seems unnecessary to send information to the client and then back to the server, but assuming you need to do that for some reason, you could use Javascript and jQuery to post a form through the $(document).ready() event, which triggers and runs its contents upon page load. So you would have your form with id="mail" and in your script you could have:
$(document).ready(function() {
$("#mail").submit();
}
you don't need any server-side code to submit on-load. also, your code doesn't look quiet right. is that in php?
There are some small syntax errors in your code:
<?php
if ($Autopost == "1")
{ ?>
<body onLoad="mail.submit()">
<form method="POST" name="mail" class="adjacent" action="./Script/addmaillist.php">
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="genre" value="<?php echo $genre; ?>" />
</form>
<?php
}
?>

Creating custom HTML templates with user input

I'm currently working on a project where the user needs to fill in a form which will be converted into HTML.
For example the user fills in the next form:
<form method="POST" action="">
Edit title: <input type="text" name="title"><br />
Edit subtitle: <input type="text" name="subtitle"><br />
Edit image: <input type="file" />
<input type="submit" value="OK" />
</form>
And the output has got to be in HTML, something like this:
<div class='container'>
<div class='leftBLock'>
<h2>*Title*</h2>
<p>*Subtitle*</p>
<img src='path/to/img/image.jpg' />
</div>
</div>
Do you guys know a way to easily get this done?
Thanks in advance!
Edit: I forgot to mention that I prefer a client-side solution.
Easy way to do is..
Store data in Table MySQL and fetch data in php file
<?php
//connection to database
//database query
?>
<div class='container'>
<div class='leftBLock'>
<h2><?php echo $rs['title']; ?></h2>
<p><?php echo $rs['subtitle']; ?></p>
<img src='<?php echo $rs['image']; ?>' />
</div>
</div>