This is the index.php:
header('Location:media.php?module=home');
I called this media.php:
<html>
<head>
<title>Test media</title>
</head>
<body>
<table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><img src="images/cms/header.png" width="780" height="77"></td>
</tr>
<tr>
<td width="200" valign="top" bgcolor="#1e2528">
<?php include "menu.php"; ?>
<p align="center"> </p>
</td>
<td width="760" valign="top" bgcolor="#FFFFFF">
<p>
<?php include "content.php"; ?>
<br>
</p>
</td>
</tr>
</table>
</body>
</html>
And this is the problem, content.php:
<table width="100%" cellspacing=5>
<?php
include_once 'include/config.php';
include_once 'admin/include/date_lib.php';
include_once 'class/class_lib.php';
include_once 'class/paging.php';
$action = new DB();
$action->db_connect();
if ($_GET[module]=='home'){
</td></tr>";
echo "<tr><td align=center>Headline News<br><br></td></tr>";
elseif ($_GET[module]=='request'){
echo "<tr><td class=judul_head>» Hubungi Kami</td></tr>";
echo "<tr><td class=isi>Silahkan hubungi kami secara online:</td></tr>";
echo "<form action='?module=sendemail' method='post'>
<tr><td class=isi>Name : <input type=text name=name size=35></td></tr>
<tr><td class=isi>E-mail : <input type=text name=email size=35></td></tr>
<tr><td class=isi>Subject: <input type=text name=subject size=50></td></tr>
<tr><td class=isi>Message : <br><textarea name=message rows=13 cols=70>
</textarea></td></tr>
<tr><td><input type=submit value=Send></td></tr>
</form>";
echo "<tr><td class=back><br>
[ <a href=javascript:history.go(-1)>Back</a> ]</td></tr>";
}
elseif ($_GET[module]=='sendemail'){
mysql_query("INSERT INTO email(name,
email,
subject,
message,
date)
VALUES('$_GET[name]',
'$_GET[email]',
'$_GET[subject]',
'$_GET[message]',
'$today_date')");
echo "<tr><td class=header_head>» Status Email</td></tr>
<tr><td class=isi>Email has been sent</td></tr>
<tr><td class=back><br>
[ <a href=index.php>Back</a> ]</td></tr>";
}
I have an email form using post method. But when I click the submit button it will be like this on url address bar
http://staging/media.php?name=Test+name&email=Test+email&subject=test+subject&,message=Test+message
Just like when I use get method. But if change $_POST
to $_GET
at the query. It doesn't work.
Is there something missing on my script? Or is it because I use the $_GET[module]
method to call on same page?
GET and POST are two different things and shouldn't be thought of as interchangeable.
http://php.net/manual/en/reserved.variables.get.php
http://www.php.net/manual/en/reserved.variables.post.php
Using $_GET variables within a PHP script allows you to parse and "get" the URL paramaters (often the query string or folder arguments with mod_rewrite), while $_POST variables allow you to collect the transfered data.
Furthermore, if you are submitting an HTTP request to an API, or your own script, it should be a POST request... because you're submitting data. If you are trying to access an API, or your own script, and only retrieving (not creating/updating/deleting) data you should use a GET request.
if ($_GET[module]=='home'){ </td></tr>";
It seems to me you're missing echo "
statement.
Since you have chosen to have the form method to be set as 'post', the values you're getting from the form in your mysql_query need to be $_POST instead of $_GET. Hoped that helped!
You're also missing a bracket before the elseif ($_GET[module]=='request')
May I recommend a syntax highlighting editor. Becomes very easy to find this stuff.
It's also not good to mix HTML output and script together.
when I click the submit button it will be like this on url address bar [...] Just like when I use get method.
Then your form is not correct. Is it the only form on the page (use View Source from your browser) and is it not nested in another form?
Also keep all the hints from the other answers in mind. While they may not immediately solve your problem, it'll become easier to hunt it down when your code is more readable.
Your setting the method of the form to be POST
and then in the following code your trying to access the data using $_GET
(GET method
) which only contains the module=sendmail
To access the data from the submitted form you need to use $_POST
(i.e. POST method
). Also the code is not properly written :
<input type="value" name="value" />
<form method="post" action="http://complete/link/page.php?media=sendmail"></form>
It is a good practice to specify complete URLs for the action
attribute.