PHP posting error - php

I keep getting a Cannot POST /app/newpost/newpost.php error when i try to post a HTML form and retrieve the data from another HTML page
HTML for the form is:
<div style="text-align:center">
<h1 style="font-size:36px; font-family:verdana;">
Create a new forum post
</h1>
</div>
<form action="newpost.php" method="POST" style="text-align:center">
<P style="font-size:24px; font-family:verdana;">Title: <input type="text" name="title" style="font-size:24px; font-family:verdana;"></P>
<P style="font-size:24px; font-family:verdana;">Description: <input type="text" name="desc" style="font-size:24px; font-family:verdana;"></P>
<input type="submit" action="window.location.href='/app/forum/forum.component.html'" style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">
</form>
PHP file is
<?php
if (isset($_POST["submit"]))
{$title = $_POST['title'];
$desc = $_POST['desc']
}
?>
im still very new to PHP someone pls help!
And how to retrieve the posted data on another html page?

You need a web server with PHP installed to test/run your PHP script. You cannot have it run from your file system directory. Depending on your environment you might want to installed something called WAMP, XAMP or MAMP.

Looks like there ; missing
i have changed you code to bellow, it should run now
<?php
if (isset($_POST["submit"]))
{
$title = $_POST['title'];
$desc = $_POST['desc'];
}
?>
try this

I think having action="newpost.php" as an attribute in the form tag and action="window.location.href='/app/forum/forum.component.html'" as an attribute in the input type="submit" tag is a contradiction. You might want to try to erase one of them.

Related

Get Image Results from API using PHP

I'm using the following api - https://habboapi.net/docs/badges/request (PHP Version) to pull Images from a Website.
It works https://beta.habbox.co/#content/habbo/badges and I have it displaying 540 "badges".
I'd like to add a search feature so you can search using the URL Parameters within the API
e.g. https://api.habboapi.net/badges?per_page=540&description=childline pulls all badges with any mention of the Word "Childline" within the description, or image name.
That way, when a user searches for keywords or descriptions, they get an output from the API.
Here is my current code:
<div class="box">
<form action="" method="post" id="10">
<div align="center">
<input style="width: 930px; height: 25px; padding: 5px; font-size: 16px; background: -webkit-linear-gradient(top, #EEEEEE , #fff); border: 1px solid #dddddd;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;" type="text" name="search" id="search" placeholder="Search Badges...">
</div>
<input style="display: none;" class="button" type="submit" name="submit" value="Submit">
</form>
</div> <br>
<div class="box" align="center">
<?php
// Fetch API Url
$api_url = 'https://api.habboapi.net/badges?per_page=540';
// Look for Badges
$badges = json_decode(file_get_contents($api_url), true);
foreach($badges['data'] as $badge){
// Echo out badges
echo '<div id="rounded" style="width:44px; height: 44px;"><img src="' . $badge['image'] . '" /></div>';
}
?>
</div>
I'm just in the early stages of learning PHP and really have no idea where to go, but I'm willing to learn and would appreciate the help on this!
To check if the user has performed a search, you can check if the search input form was posted:
$search = isset($_POST['search']) ? $_POST['search'] : '';
Then, you can just append the value to the URL:
$api_url = 'https://api.habboapi.net/badges?per_page=540&description=' . $search;

Html form into a Php table

So, im trying to get the informatino you put into an HTML into a Table in Php, problem is, i cant find any solutions online. and i dont know much about php yet. this is only my second time asking something so i still dont quite know the way you are supposed to fill in a question. anyway, here is the code in question:
HTML
</head>
<body>
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
<div>
<label for="vnaam">Voornaam</label>
<input type="text" id="vnaam" name="vnaam" required/>
</div>
<div>
<label for="anaam">Achternaam</label>
<input type="text" id="anaam" name="anaam" required/>
</div>
<div>
<label for="tnum">Telefoon Nummer</label>
<input type="text" id="tnum" name="tnum" />
</div>
<div>
<label for="tnum">E-mail</label>
<input type="email" id="tnum" name="tnum" />
</div>
<div>
<label for="adres">Adres</label>
<input type="" id="adres" name="adres" required/>
</div>
<div>
<label for="land">Land</label>
<select id="land" name="land">
<option value="ned">Nederland</option>
<option value="usa">Amerika</option>
<option value="eng">Engeland</option>
<option value="bel">Belgiƫ</option>
<option value="fr">Frankrijk</option>
<option value="ger">Duitsland</option>
</select>
</div>
<div class="button">
<button type="submit">Opsturen</button>
</div>
</form>
</body>
</html>
Css
<style type="text/css">
form {
/* Just to center the form on the page */
margin: 0 auto;
width: 400px;
/* To see the outline of the form */
padding: 1em;
border: 1px solid #CCC;
border-radius: 1em;
}
form div + div {
margin-top: 1em;
}
label {
/* To make sure that all labels have the same size and are properly aligned */
display: inline-block;
width: 90px;
text-align: right;
}
input, textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
/* To give the same size to all text field */
width: 300px;
-moz-box-sizing: border-box;
box-sizing: border-box;
/* To harmonize the look & feel of text field border */
border: 1px solid #999;
}
input:focus, textarea:focus {
/* To give a little highlight on active elements */
border-color: #000;
}
textarea {
/* To properly align multiline text fields with their labels */
vertical-align: top;
/* To give enough room to type some text */
height: 5em;
/* To allow users to resize any textarea vertically
It does not work on all browsers */
resize: vertical;
}
.button {
/* To position the buttons to the same position of the text fields */
padding-left: 90px; /* same size as the label elements */
}
button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: .5em;
}
</style>
<form method='POST' action='formhandler.php'>
<input type='text' name='name' value='john'>
<input type='text' name='address' value='32 flowers street'>
<input type='text' name='email' value='john#example.com'>
</form>
A typical form like this one will be delivered to the php file formhandler.php by the browser.
Then you handle it in your formhandler.php file
formhandler.php
$name = isset($_POST['name']) ? $_POST['name'] : "";
$address= isset($_POST['address']) ? $_POST['address'] : "";
$email= isset($_POST['email']) ? $_POST['email'] : "";
echo $name; //john
echo $address; //32 flowers street
echo $email; //john#example.com
Now you have the form data. use it as you like, store them in a database, build another html response file to the user or do what ever you want to do with them.
for example, we are going to feedback the data to the user.
echo "We have successfully received your inputs as <br>
<table>
<tbody>
<tr><td>name</td><td>$name</td></tr>
<tr><td>address</td><td>$address</td></tr>
<tr><td>email</td><td>$email</td></tr>
</tbody>
</table>";
Note: in a real life production you need to validate any inputs come to your php files first.
First change
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
to Something like
<form action="http://www.cs.tut.fi/getdata.php" method="post">
Because you are using cgi-bin/run~jkorpela/echo.cgi which is not used for get php data because its a cgi file. it should a .php extension file and php should be installed on your server. and I am assuming that http://www.cs.tut.fi/ is the url for your website.
Where getdata.php is the php file where you get all your input values. You need to create that file on your server. If you are on AWS then it might be something like /var/www/html/
getdata.php
<?php
echo "<pre>";
print_r($_REQUEST);
?>

Wordpress Template causes 404 Error

I've created a page in Wordpress and used this form as template. But when I submit the form it shows Error 404 page not found.
I've tried various scripts in php but I didnt get fruitful results. Hoping for the best answers. Thanks :)
Here is my coding
<?php
/*
template name: visit
*/
$servername= "localhost";
$username="root";
$password="";
$database="wp_chandan";
if(isset($_POST['name']) && isset($_POST['email']))
{
$name=$_POST['name'];
$email=$_POST['email'];
var_dump($email);
#mysql_connect($servername,$username,$password);
#mysql_select_db($database);
$query=mysql_query("insert into visitor_d (NAME,Email) values ('$name','$email') ");
} ?>
<html>
<head>
<?php get_header(); ?>
<style type='text/css'>
/* form elements */
form {
margin:100px;
padding: 10px 2px;
background: #F5F5F5;
width: 50%;
height:auto;
border:1px solid green;
}
input {
padding:2px;
border:1px solid #eee;
font: normal 1em Verdana, sans-serif;
color:#777;
align:center;
}
input.button {
font: bold 12px Arial, Sans-serif;
height: 24px;
margin: 0;
align:center;
padding: 2px 3px;
color: #333;
background: #e7e6e6 url(MarketPlace-images/button.jpg) repeat-x;
border: 1px solid #dadada;
}
</style>
</head>
<form action="" method="POST">
<fieldset>
<legend><b><u>Visitor Details:</u></b></legend>
Name: <input type="text" name="name" placeholder="Fill Name" required><br><br>
Email: <input type="text" name="email" required placeholder="Enter Email"><br><br>
<input type="submit" name="submit"><br>
</fieldset>
</form>
<?php get_footer();?>
</html>
Make sure that you don't have any reserved WordPress terms in the name attributes in the fields of the form.
WordPress uses many of these reserved terms for it's internal rewriting and as a result if you using name attributes such as "name", it breaks the WordPress rewrite rules and causes this 404 Not Found page after submitting the form.
See more here: http://www.lost-in-code.com/platforms/wordpress/wordpress-404-not-found-when-submitting-form/
html5 doesn't like the empty action attribute, it does cause issues in some browsers. you have not selected a standard so i assume the browser will default to html5
<form action="#" method="POST">
Setting the action as # should work but one thing to watch out for is actions added before your page template is loaded, this can obviously grab the $_POST and redirects would be common so that may also be a reason.
also
Name: <input type="text" name="name" placeholder="Fill Name" required>
WP does not like when you call a field "name" it conflicts with something (i have forgotten the reason by now)
Name: <input type="text" name="aname" placeholder="Fill Name" required>

PHP will not connect with mysql database

Can anyone see a reason why this would be failing to connect with / write to a mysql database? I can't be sure which but I know the data isn't entering when the form is submitted. It just routes to a blank screen.
The html
<html>
<div style="width: 330px; height: 130px; overflow: auto;">
<form STYLE="color: #f4d468;" action="send_post.php" method="post">
Category: <select STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
name="category">
<option value="blah1">blah1</option>
<option value="blah2">blah2</option>
<option value="blah3">blah3</option>
<option value="blah4">blah4</option>
</select> <br>
<textarea overflow: scroll; rows="4" cols="60" STYLE="color:
#919191; font-family: Veranda; font-weight: bold; font-size: 10px;
background-color: #000000; width:300px; height:80px; margin:0;
padding:0;" name="contents">
</textarea><br>
<input type="submit" STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
value="Create Log">
</form>
</div>
</html>
and send_post.php
<?php
//Connecting to sql db.
$connect=mysqli_connect("localhost","username","password","mysqldatabasename");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO mytablename (category, contents)
VALUES ('$_POST[category]', '$_POST[contents]')";
?>
I'm new to this but I researched hard and now I'm at an impasse. I've tried entering code that checks for connection errors but I always just get a white screen and no db updates. I would greatly appreciate help. (Also do the quotes stay included in "localhost", "username" etc?)
Seems to be a simple syntax error.
Missing a closing ) here:
mysqli_query($connect,"INSERT INTO mytablename (category, contents) VALUES ('$_POST[category]', '$_POST[contents]')");
The arguments for the mysqli_connect do need to be enclosed in quotes. You also might want to seriously consider sanitizing your inputs. This form is wide open.

using php and java script in a form

I am a little bit lost. What I want to achieve is:
my own custom button
change onMouseOver etc'
keep it's size
post the information to a php server side code
What I'm missing is:
The post - I couldn't figure out how to combine js & php
The Button size - my code sets a size for the original button but after the rollover it changes
The code:
<html>
<head>
</head>
<body>
<script>
function form_on_click(frm)
{
document.buttonMore.src='bottom_more_click.JPG';
frm.submit();
}
</script>
<div style="position: absolute; left: 120px; top: 90px; background-image: url(myBackgroundPicture.jpg);
background-repeat:no-repeat; width: 800px; height: 280px; padding: 15px;">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="whatever" size= "55" height="100" lang="en" dir="ltr" style="margin-top: 188px; margin-left: 95px; height: 20px; background-color: transparent; border:none;
color: #FFFFFF; font-family: Verdana; font-weight: none; font-size: 18px;">
<a onmouseover="document.buttonMore.src='bottom_more_hover.JPG'"
onmouseout="document.buttonMore.src='bottom_more_reg.JPG'"
onmousedown= "form_on_click(this.form) this.form.submit()"
onmouseup="document.buttonMore.src='bottom_more_hover.JPG'">
<img src="bottom_more_reg.jpg" name="buttonMore" height="30" width="173" border="0" alt="MORE!" style="margin-bottom:-10px; margin-left: 15px; height: 30px; width: 100px;">
</a>
</form>
</div>
</body>
UPDATED:
Oh sure.. excuse me. You're totally right. I don't know where do I have my head.
Try something else, remove this line again
onmousedown= "form_on_click(this.form) this.form.submit()"
And replace it by
href="javascript:form_on_click(this.form);"
Just a suggestion, why don't you use an...
<input type="button" />
...and then customize it in CSS?
EDIT
About the size of the button, are those jpeg files equally sized?
The easiest way would be to use JQuery. All of what you want to do can be done with just a few simple steps in JQuery. The mouseover/out submit all of it and tons more.
I fell in love with JQuery...

Categories