Post HTML <form> on load - html

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
}
?>

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 to show double quote in input tag?

How to show double quote in input tag ?
my $test is i test "hello"
and input like this
<input name="test" type="text" value="<?PHP echo $test; ?>">
When test code in input show only i test
How can i do for show i test "hello" in input tag ?
Try to use htmlspecialchars or use htmlentities
<input name="test" type="text" value="<?php echo htmlspecialchars($test); ?>">
<input name="test" type="text" value="<?php echo htmlentities($test); ?>">
Or by using PHP echo you can do it
<?php echo '<input name="test" type="text" value="'.$test.'">'; ?>
use html_entity_decode since you're using php like so:
<input name="test" type="text" value="<?PHP echo html_entity_decode($test); ?>">

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" />

Getting results from database into textarea

I have an edit page that fills the content with the original content from the database, I am using inline php this populates the field with the title, and all the other fields work as well. When i try and fill the textarea using the same method it doesn't work.
All the other fields are varchar except the textarea which is text.
The php is in the value of the form.
require_once('includes/db.inc.php');
$stmt = $mysqli->prepare("SELECT
postID,title,content,author,image
FROM posts where postID = ?");
$stmt->bind_param("i",$_GET['postID']);
$stmt->execute();
$stmt->bind_result($postID,$title,$content,$author,$image);
$stmt->fetch();
$stmt->close();
?>
<section id="createPost">
<form method="post" action="editPost.php">
<fieldset>
<legend>Edit Post: <?php echo $title ?></legend>
<input name="postID" type="hidden" value="<?php echo $postID; ?>">
<label for="titleOfPost">Title of Post:</label><br />
<input type="text" name="titleOfPost" size="82" placeholder="Enter title of post" required value="<?php echo $title ?>"><br />
<label for="bodyOfPost">Content of Post:</label><br />
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed" value="<?php echo $content ?>"></textarea><br />
<label for="authorOfPost">Author:</label><br />
<input type="text" name="authorOfPost" size="82" placeholder="Author name" required value="<?php echo $author ?>"><br />
<label for="imageOfPost">Image:</label><br />
<input type="text" name="imageOfPost" size="82" placeholder="image" value="<?php echo $image ?>"><br />
<input type="submit" name="newPostBtn" value="EditPost" id="newPostBtn"/>
</fieldset>
</form>
</section><!--end createPost-->
Textarea element doesn't a have property value. Use:
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed"><?php echo $content ?></textarea>
Textareas aren't populated like other input types. The content goes between the tags (like an anchor tag) not within the opening tag (like an image tag).

Simple script issue?

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>