PHP send form info to 2 different files - php

I'm having a bit of a problem with a form. I need to send its information to 2 different pages for two different uses. One of the pages will show what was inserted in the form and the other one will use the form's title as a link to the other page.
The code I'm using is below. Right now, it only takes the form info and shows it in the View.php page.
I'm pretty sure I'll have to use some PHP in here, but I'm out of ideas.
<html>
<head>
<title>!!!!Protótipo de Formulario de Noticia!!!!</title>
<style>
textarea{
resize:none;
}
</style>
</head>
<body>
<form method="post" action="View.php">
<p>Titulo: <input type="text" name="Titulo" required/></p>
<p>Digite o texto da noticia abaixo:</p>
<textarea rows="10" cols="50" name="Noticia" required></textarea>
<br>
<input type="submit" value="Publicar" onsubmit="location.href='View.php;'" />
</form>
</body>
</html>

you do not need
onsubmit="location.href='View.php;'
The simplest approach is to submit to one page, which acts as a controller. It can process the data, and selects a template for displaying the next page.
If needed, you can also pass data across pages with either a session, or http query params (url?id=1234)

Related

HTML PHP FORM two actions?

I have a HTML form where after the user submits the form they see my FormSubmit.php thank you page. Is there a way to make that thank you page still display after submission, but also add a link to another php page that shows you a list of who added their names?
I created a SignupList.php page that reads from a text file. I added the link to the form HTML page at the bottom and to the FormSubmit.php thank you page, but when I add a name ,reach my FormSubmit.php thank you page, then click the link to my SignupList.php page there are no names on it. I noticed on the HTML page in the form action section when I replace FormSubmit.php with SignupList.php I get my desired results a name displays after hitting submit, but my thank you page is gone since I removed the action to it. Any suggestions on how I can do both? Please let me know if I need to explain further.
<!DOCType html>
<html>
<head>
<title>Form</title>
<html lang="en">
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
</head>
<body>
<form method="post" action="FormSubmit.php">
<p>Please sign up</p>
<p> First Name:<br>
<input type="text" name="firstname" size="30">
</p>
<p> Last Name:<br>
<input type="text" name= "last name" size="30">
</p>
<p>
<input type="submit" value="Submit Information">
</p>
View Sign Ups
</form>
</body>
</html>
edit: I am not sure why i was down voted, but i figured out the issue. I needed to edit my FormSubmit.php file to write to the document.
In your Formsubmit.php, add:
header('Refresh: 3;url=page.php');
which will refresh the header and redirect the page after 3 seconds.

How to use "HTML form target self"?

I have a database with some numbers assigned to a fake account (PHP).
I try to contact the database (with success) and get the right result from the form.
My issue is that the form result open a new page and display there...
I would really like the result to be displayed IN the module I use to send the form OR anywhere else on the same page I used to send the form.
<!DOCTYPE html>
<html>
<body>
<form
method="post"
action="http://ggdbase.dx.am/impulseGetInfo.php"
target="_self">
Account name:<br>
<input type="text" name="name" value="derps">
<br>
<input type="submit" value="Click To Load Account Info">
</form>
</body>
</html>
This is what the module look like (on Enjin.com)
This is what I get when clicking the button
I did try replacing '_self' with '_blank' or parent and all the other options I could find but none of them gave me a different result :S
Could it be caused by the Enjin system itself ?
Do not use target. And replace the action as
action=""
This will ensure that you are calling the same page. Write the PHP code there itself.
Hope that help!

Html Post Method get blank page after refresh

I am testing Html form using post method and got this odd result:
I have two html pages on server apache (already installed php): post.html and target.html, and the code for these page are followings:
post.html (don't worry about the html standard)
<div style="text-align: center">
<form method="POST" action="target.html">
<input type="text" name="testname" value="sometext" />
<input type="submit" value="submit" />
</form>
</div>
and target.html
<html>
<head>
</head>
<body>
<h1>Target page</h1>
</body>
</html>
When I entered data to the form on post.html page and hit submit button, I got to the target.html page. When on this page(target.html), I refreshed the page, and what I receive is a blank page. The second time I refreshed, It turned to normal Html page.
I don't know why it returned a blank page the first time I refreshed, I have tried the same approach but with PHP page, and the content of target page (assum name target.php) still remains (not blank like html files above)
So, could you explain me about this problem?
Thank you.
This definitely has something to do with your browser. Same here on a mac using Safari, on some pages after submitting the content, the page seems to freeze, I refresh it, and then it works again.
Definitely not a code problem, as far as I'm concerned.
It's because you cannot pass an input from html to html file. Your target.html should be a php file (target.php).
and try to put this code on your target.php
<?php
var_dump($_POST); //this will show the inputted text and also show the data type and will stop the code execution here. other codes below will not be executed.
echo $_POST['testname'];
?>
Additionally, change target.html to target.php in your form action
First I will start out by correcting your post.html
<div style="text-align: center;"> <!-- added a ; after center -->
<form method="POST" action="target.html">
<input type="text" name="testname" value="sometext" />
<input type="submit" value="submit" />
</form>
</div>
that may not matter but you should end all your styles with ;
To continue, everything looks fine. maybe something weird happened between refreshes.
save your form page in php than add the php script in the same page, here try this. remember save in .php.
<?php $testname = $_Post['testname'];
echo $testname ?>
<div style="text-align: center">
<form method="POST" action="">
<input type="text" name="testname" value="sometext" />
<input type="submit" value="submit" />
</form>
</div>

Avoiding duplicate code between php and javascript

I have a php page with a form on it for adding people to a small group.
For each person being added, there is a with multiple form elements, each named according to the person's number. For example:
<div class="user">
<input type="text" name="user1LastName" />
...
</div>
<div class="user">
<input type="text" name="user2LastName" />
...
</div>
For each person in the database, the php page populates a form sections.
To add additional people, the user can click on a "+" icon, at which time the page uses jQuery to dynamically populate a new . To do this I am simply appending the new div html to the existing form. This means that the javascript page contains all the same html markup (to be appended), as the php page.
This seems like an unnecessary duplication of code. Whenever I change something in the php page, I also have to change it in the javascript code.
Is there any general method for avoiding such code duplication? The only thing I can think of is to use jQuery to grab the html from an already existing div. But in this case, the values of the form fields for user n will appear in the new code for user n+1.
Thanks.
Capisci :)?
<div class="user" id="user_1">
<input type="hidden" name="uid[0]" value="1"/>
<input type="text" name="lastname[0]" value="user480029"/>
...
</div>
<div class="user" id="user_2">
<input type="hidden" name="uid[1]" value="2"/>
<input type="text" name="lastname[1]" value="arto"/>
...
</div>
Now when adding another field just...
<div class="user" id="user_3943094103945">
<input type="hidden" name="uid[]" value=""/>
<input type="text" name="lastname[]" value=""/>
...
</div>
Then you iterate trough $_POST[] a do what you want.
You have user ID on .user, so I you delete user you can remove that part of HTML (this is more for UX), more importantly, you don't have hundreds of variables but just a few array which you can iterate in one loop. Hope you get the point. Cheers.
The php code should give the javascript a "prototype", which could be modified using javascript. That way, even if there aren't any users, the javascript would still work. This example is obviously missing lots of code (like forms), but it should give you an idea. I haven't tested it because I assume you have to make lots of modification anyways.
<script type="application/x-javascript">
addEventListener("load",function(){
document.getElementById("add-user").addEventListener("click",function(){
var node=document.getElementById("prototype-container").getElementsByClassName("users")[0].cloneNode(true),n=document.getElementById("add-user").getElementsByClassName("users").length,list=node.getElementsByTagName("input");
document.getElementById("user-list").appendChild(node);
node.id="users_"+(n+1);
for(var i=0;i<list.length;++i)
list[i].name&&(list[i].name+="["+n+"]");
},false);
},false);
</script>
</head>
<body>
<div id="prototype-container">
<? /* print out a div without any information in it */ ?>
</div>
<div id="user-list">
<? /* print out divs with some infomation in them */ ?>
</div>
<button id="add-user">add a user</button>

PHP sticky form question

I'm using a PHP sticky form and was wondering is there a way I can clear a forms fields once its been submitted by the user?
A sticky form is simply a standard HTML form that remembers how you filled it out. This is a particularly nice feature for end users, especially if you are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).
You could have some default values and update the fields to the values using Javascript , in case you do not want to reload the page. If you do want to reload the page, and use PHP sticky form, populate the form after clearing out the GET values.
If you want it to clear the data, you can do this:
<html>
<head>
<title>A Self-Clearing Form</title>
<script>
function clearForms()
{
var i;
for (i = 0; (i < document.forms.length); i++) {
document.forms[i].reset();
}
}
</script>
</head>
<body onLoad="clearForms()" onUnload="clearForms()">
<h1>A Self-Clearing Form</h1>
<form method="post" action="page2.php" name="test">
<input name="field1"/> Field One
<p>
<input name="field2" type="radio" value="One"/>One
<input name="field2" type="radio" value="Two"/>Two
<input name="field2" type="radio" value="Three"/>Three
<input name="field2" type="radio" value="Four"/>Four
<p>
<input type="submit" value="Submit Form Data"/>
</form>
</body>
</html>
This will clear the form when the HTML body loads completely.
It depends on your implementation, but the solution probably involves clearing the session variables that are used to hold the form data. If you're using a cut and paste solution I would suggest reading the documentation further or maybe looking over the code in detail to see how everything is stored.
If you want to check the session variables to see if the form data is in there you can use:
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
die; // this line is optional

Categories