The question looks quite similar, however it's not been answered anywhere. What I want is I'm designing a website with login option. There is a particular page named "announcements.php" where I want site visitors to be able to see announcements made by admin. So, In order to do so I have made a page which only admin can access and add an announcement ("newbroadcast.php"). The page contains a form where admin can write an announcement and submit it. However that announcement should be accessible to all the users which visit announcements.php
Code (newbroadcast.php)
<div class="newbroad">
<img src="content/images/broadcast.png" align="center" width="160px" height="160px">
<form method="post" action="announcements.php">
<strong>BROADCAST : </strong>
<input type="text" name="broadd" placeholder="Write your new announcement here...">
<input type="submit" name="submit"> <input type="reset" name="reset">
</form>
</div>
Code (announcements.php)
<div class="markcontent">
<center><h1 class="whiteboard">CDC DEPARTMENT WHITEBOARD</h1></center>
<hr>
<?php
$mynewbroad=$_POST["broadd"];
?>
<marquee behavior="scroll" direction="down" scrollamount="2" onmouseover="this.stop();" onmouseout="this.start();">
<center>
<?php
echo $mynewbroad;
?>
</center>
</marquee>
What is actually happening?
Everything works fine, however when admin writes a new announcement or broadcast it gets posted on the page ("announcements.php"), where I actually want to post it. However that is not permanent. Whenever I open the page itself it shows an error => Undefined index: broadd
Is their any way to permanently show the broadcasts made by admin? What is the possible solution to this error?
POSTing data to a PHP page does not automatically "store" the information you are posting to it. It merely sends the data from newbroadcast.php to the announcements.php, and announcements.php can do what it is programmed to do with the data. In your case, announcements.php is displaying the data - but not storing it for future.
To store the data for future, you must save it somewhere on the server and re-factor announcements.php to check on the server for such data, and (if such data is found) display all the announcements via some sort of loop.
So how to store the announcements?
You can use PHP commands such as fwrite or file_put_contents to save the announcements into a specific folder, and then write a loop to read all the file in that folder and spit them out onto the page.
But the better way is to use a database.
References:
http://www.tutorialrepublic.com/php-tutorial/php-mysql-insert-query.php
https://www.youtube.com/watch?v=mpQts3ezPVg
Related
I have a form with one text input field:
index.html
<form method="post" action="comment.php">
<a href="javascript:void(0)">
<div class="avatar" style="background:url('img/user4561.jpg') center/cover"></div>
</a>
<input name="comm" type="text"/>
<input type="submit" value="submit"/>
</form>
And here is comment.php in the same directory
<?php
echo $_POST["comm"];
?>
Now the most incredible event. The form data does not get submitted! I get an error:
Notice: Undefined index: comm in 192.168.0.1/comment.php on line 2
I changed comment.php like this:
<?php
if(isset($_POST["comm"])) {
echo "It is set";
} else {
echo "It is not set";
}
?>
and I get:
It is not set
So, I copied the HTML code of the form into another blank webpage. I also changed the action attribute of the form to "192.168.0.1/comment.php". Now I get
It is set
I also tried using GET instead of POST, but I am again in the same conflict.
EDIT: I removed all other files and code, except for the form and its script. The problem persists. You can now read and modify the source code. Go to files.000webhost.com. Use chatstack as the website name and 6RxOkuJ1CIkbNqxKUMGr as the password.
EDIT: I modified the code for the form above to exactly match that in the above given link. I noticed that removing the link from inside the form solves the problem. I have already done this, but this is so weird. According to this post it is totally fine to have other elements inside a <form> element. There are also other parts of my website where I have <div> elements inside a form element and the form is working fine.
What is this? Is it the server or something else?
I am pretty sure the information provided here is not enough. Feel free to ask for any additional information. There is just too much information to write in a post, and I don't know which part of it is relevant.
Open your dev-console in browser.
Go to the network tab. Now you fill in your form and submit. After your comment.php is loaded, you check for the very first row in the network tab (should be the comment.php html document).
When you click on that request it should display you, whether the request was GET or POST and also what data were sent to this document.
You can also try at comment.php var_dump($_REQUEST); to see what data were sent and how it can be accessed.
By this you can see if server all over receives any data. If yes, then you go for an other server like apache2 for testing purpose to look if bug is fixed.
That's how I would to that, but I suspect that by editing your code for the public, you accidentally withheld some information that would solve this simple problem.
It's very unlikely that web-servers like WAMP had passed the testing environment before publishing a new version allthough no data could be procesed by server
I have a CodeIgniter form, that looks a bit like this in code:
<?php echo form_open_multipart('admin/updateperson', array("id" => "updateform")); ?>
<input placeholder="First name" name="fname" id="fname" value="<?= form_prep($person->fname) ?>" />
<?= form_submit("save", "Save"); ?>
<?php echo form_close(); ?>
I want to be able to hit Save, and have it go to a Controller which will save the uploaded data to the database, and then forward me on to the Controller that showed the form in the first place, which will return me to that same page, displaying the updated details.
I'm currently doing it with limited success with a redirect call in the upload controller, so CodeIgniter just routes the request as normal to the original Controller that reads from the database and displays the form as in the first place. This sort of works.
However, there's a bug: while it does update the data in the database, and it shows me the original form, it shows me the previous values! These can be cleared by hitting F5 or going into the page again, but looks bad.
Is there a better way to do this? I'd think about using set_form, but that's only designed for when validation fails, apparently?
If you're using a redirect after the form submission you need to store all data you need to keep to be visible in the next page,
you can store data in Session, Cookie, Url or HTML5 local and/or session storage
As you preferr :)
I developed a web crawler to search for certain tags on my companies websites to make sure they are live, have Google analytics, blah blah. However, my company has close to a hundred websites so the actual crawl process, is literally a crawl. So I wanted to create a form where the user inputs a web address of one of our companies and it only crawls that one website. I am not good with forms, so what I basically want the form to do is store the url the user inputs then redirect to a different page where the url is given to the crawler and the results are shown.
Here is basically what I have so far, not much, I am having trouble redirecting to a different page and storing the URL variable so I can pass it to the crawler code that I have.
<div id="main-content" class="mc-left"> <div class="entry"> <div style="position:absolute; margin-left:520px; height:25px; width:120px; font-size:10px;"> </div>
</div>
<h2><?php the_title(); ?></h2>
<form name="form1" id="form1" method="POST" action="submitcrawler.php">
<div class="hiddenfields">
<p>Website Address:<br>
<input name="websiteaddress" type="text"></p>
<input type="submit" class="submit" name="submit" value="Submit">
</form>
As you can see I want this form to bring me to submitcrawler.php, however, when I create that php file, when I hit submit it brings me to the current slug (../crawler-2/submitcrawler.php instead of ../submitcrawler.php) so it throws up a 404 error
The form is submitting to 'submitcrawler.php' in the same folder as the file that you're looking at, so if its in /crawler-2/ then that's where its looking.
Use ../ if you want to ascend to the directory above, or probably better, use / and enter the path to the file from the web root (the top directory viewable by apache / the web server).
So
<form action="../submitcrawler.php">
or
<form action="/submitcrawler.php">
For the functionality that you're looking for, you could try using method="GET". That way, you can see the information that is being passed to the other PHP script in the URL.
Then simply retrieve the information in the other PHP script:
if(isset($_GET['websiteaddress'])) {
$websiteaddress = $_GET['websiteaddress'];
} else {
echo "No web address was received.";
}
In terms of the form action attribute, you need to use an absolute path if the scripts will both be static, otherwise if the scripts are dynamic and may change locations on the servers, then use relative paths.
Path Info:
http://en.wikipedia.org/wiki/Path_%28computing%29
http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm
If the file you're talking about is under crawler-2 directory it will submit the form to that file unless you use a relative path ../submitcrawler.php
The action you have set on that form will send it to submitcrawler.php in the same directory as the current script. Try changing the action to ../submitcrawler.php, or alternatively set it to the absolute url of the script (http://mydomain.me/submitcrawler.php)
You used a relative path in your post action value. If for example your crawler script is in your webroot you should use action="/submitcrawler.php". If not you can do something like action="/path/to/submitcrawler.php"
I have a question about login and accessing database for mobile web applications.
Namely, the fact that all the pages are usually in one page separated by ids, how do you send form data using PHP? There are no separate files to send data to.
Doesn't opening a connection at the top of the HTML file without logging in cause a security problem? If they do not login successfully, you are essentially redirecting to the same HTML file.
If someone could redirect to a tutorial (which I can't seem to find online) or give an overview, that would be awesome.
<?php
//1. Open connection
//2. Select database
?>
<html>
<body>
<section id="page1">
<form action="page2" method="POST"><!-- since the data needs to be send to #page2, do I just POST to the same html file? -->
</form>
</section>
<section id="page2">
<?php
//3.query database
//4. display results
?>
</section>
<section id="page3">
</section>
</body>
</html>
<?php //5. close connection?>
1) You can keep separate files in PHP, they don't have to be in the same page. For example, you have a file named login.php.
<?php ?>
<html>
<form action="index.php" method="post>
<input type="text" name="username"/>
<input type="password" name="pasword"/>
</form>
</html>
Then in your index.php file you would have something like this.
<?php
if($_POST['username']=="admin" && $_POST['password']=="admin")
//query database
//display results
?>
So you don't have just one file containing different pages.
2) You can include a main.php file in all your pages and check if user is logged in, if not, redirect him to some other page. In adition you could learn a little bit about session variables.
<?php
include("main.php");
?>
In your main.php
<?php
session_start();
if(!$_SESSION['islogged'])
header("location: otherpage.php";)
?>
So if the user is logged in there will be no redirection but other way it will.
Hope this helps you.
1)
Check out the following link. http://www.w3schools.com/php/php_post.asp
It explains how to access form data that has been sent to your php page. You need a decent understanding of HTML before you ever start working with PHP.
2)
The user isn't the one logging in. They never see the php code. It stays on the server. What they see is HTML. The connection is so the php page can connect to the database and retrieve information.
I'm trying to create a pretty standard user creation form for a website I'm working on, but my PHP amateur status is preventing me from accomplishing my goal.
Essentially what I want to do is have the user fill out a registration form, and then have a server-side script attempt to register that information. If the registration succeeds, then the user will be redirected to a page telling them to look for a verification email. If that registration fails (the email address is already in the system, not matching passwords, whatever), I would like to reload the page, but display the error message to the user above the form. Currently the form I have is being echoed out of a php file which is separate from the HTML from where the page is stored. My three questions are this.
From what I've read, the way to redirect users on success is to use header("Location: http://foo.com"). Is that correct, or is there a more proper way to do it?
How can I get access to the error(s) that caused the first user to fail? I've read that setting a session variable is a poor idea for a number of different reasons, but without that how can I keep track of the errors thrown by the form validation? Should I just create a global $errors variable that I clear every time I echo the registration form?
For this case, should the page that the registration form is on have a .html extension or a .php extension, or does it not matter? More generally, is there any rule for when a page is .html vs .php/.asp/something else?
Edit: added code below. Note, the form is rendered from a file called in ../views/register_form.php (I assume this is how I'd work on getting something MVC-like in PHP)
The page that the form is rendered on (called index.html)
<body>
<script type="text/javascript"> <!-- put the jquery load stuff into here -->
$(function(){
$("#registration_form").load("views/register_form.php");});
</script>
<div class="topbar"><!-- Create the top bar layout-->
<div class="fill">
<div class="container">
<a class="brand" href="#">Website!</a>
<ul class="nav">
<li class="active">Home</li>
<li> About</li>
</ul>
<form action="scripts/login_user.php" method="post" class="pull-right">
<input class="input-medium" id="login-email" type="text" name="email_addr" placeholder="email" />
<input class="input-medium" id="login-password" type="password" name="password" placeholder="password" />
<button class="btn" type="submit">Login now!</button>
</form>
</div>
</div>
</div>
<div class="container">
<div class="content">
<div class="page header">
Page header!
</div>
<div id="registration_form">
</div>
The script
if(//some of the data in the form isn't set)
{
//Set error conditions based on missing data
}
$confirm_password=$_REQUEST['confirm_password'];
$password=$_REQUEST['password'];
if($confirm_password != $password){
//report passwords don't match}
$email_addr=$_REQUEST['email_addr'];
$first_name=$_REQUEST['first_name'];
$last_name=$_REQUEST['last_name'];
try
{
$successful_creation=create_user_and_send_verification_email($email_addr, $password, $first_name, $last_name);
}
catch(Exception $e)
{
/*SQL errors are caught here*/
}
if($successful_creation==FALSE)
{
//If it couldn't be inserted for a non exception generating reason. If I get here, should I echo a page that has those errors printed, but is otherwise identical to the previous one?
}
else
{
//redirect to a "wait for a verification email" page
}
Yes that is the correct way to do it, but all headers must be sent before you try to render anything to the page. Also, you don't necessarily need to redirect, you can generate the correct output with PHP depending on the form data.
The script that receives the submitted form should check the submitted data for errors and can then output those errors to the page that is generated by that script.
If the page has php code in it then usually it needs a php extension unless your server is set up to process html pages as php. Otherwise, .html is fine.
I hope that helps point you in the right direction, although a more focused question with the code you have up to now would generate a better answer.
There are lots of tutorials on this on the web, you should follow one of those and experiment.
why dont you write the server side code on the same page and for every form input use a flag
for eg: if email is not valid $email=1.
den after submission if any flag is initialized,check for the flag and if $email=1 echo error else redirect.