Move <input> datas between different pages - php

In the same server, I have a page called mkwars and another called generatedtab. Inside mkwars I have a lot of input fields that contains numeric numbers.I need to transfer the datas from those inputs, to another new inputs located in the page generatedtab.
This is the HTML code:
<table border="0" id="table1" align="center" cellspacing="0" cellpadding="3" width="100%">
<tr>
<td width="50%" valign="top"><b>Home Clan:</b> <input type="text" id="clan1" name="clan1" onchange="nomewar();"/></td>
<td><b>Opponent Clan: </b> <input type="text" id="clan2" name="clan2" onchange="nomewar();"/></td>
</tr>
</table>
//other code
<form method="post" action="savewar.php">
<input type="submit" Value="Generate Table" style="height:70px;width:800px" />
</form>
And here you can see the PHP file:
<?
$percorso = file("war/filedb.txt");
while(list(,$value) = each($percorso)){
list($clan1, $clan2) = split("[:]", $value);
$params["clan1"] = trim($clan1);
$params["clan2"] = trim($clan2);
#print results
echo $params["clan1"]." - ".$params["clan2"]."<br />";
}
?>
war is a folder inside my server. When I click the button Generate Table I can't see the file (war/filedb.txt). Could you help me? I thought that the PHP way was the better, but if you think that I should do something else, tell me.

I'm not exactly clear on what you're trying to do here. I think you want to fill out the html form and have the php script save the new input into a file on the server, and then print out the contents of the file. If that's correct, here are a few things you need to fix.
1) On your html page, the <form> tag must enclose all of the input fields you want to post back to the server. so:
<form method="post" action="savewar.php">
<table border="0" id="table1" align="center" cellspacing="0" cellpadding="3" width="100%">
<tr>
<td width="50%" valign="top"><b>Home Clan:</b> <input type="text" id="clan1" name="clan1" onchange="nomewar();"/></td>
<td><b>Opponent Clan: </b> <input type="text" id="clan2" name="clan2" onchange="nomewar();"/></td>
</tr>
</table>
<input type="submit" Value="Generate Table" style="height:70px;width:800px" />
</form>
2) In your php script, you need to use the superglobal $_POST or $_REQUEST variable to catch the data from the posted form. For example:
$clan1 = $_POST['clan1'];
$clan2 = $_POST['clan2'];
3) In your php script, you need to open the file for writing and append the new data to the end of the file:
$fileappendpointer = fopen("war/filedb.txt",'a');
$newline = $clan1 . " - " . $clan2 . "<br>";
fwrite($fileappendpointer, $newline);
4) Then you can easily read out the contents of the file:
fclose($fileappendpointer);
$filereadpointer = fopen("war/filedb.txt",'r');
$contents = fread($filereadpointer,filesize("war/filedb.txt"));
fclose($filereadpointer);
print $contents;

Related

No data submitted from a form

I have created a simple HTML form containing just one field. When I press submit some PHP code that I have written gets called and outputs text that would include submitted data if everything was working. But no submitted text gets printed by the PHP. The form has been created on a Godaddy HTML page and the form is as follows:
<FORM BORDER="1" action="http://www.bestpro.com.au/wordpress/PHB_action.php"
method="post" enctype="application/x-www-form-urlencoded"
eenctype="multipart/form-data" name="PHBForm" accept-charset="ISO-8859-1"
ienctype="text/plain">
<TABLE>
<TR>
<TD>First name:</TD><TD><INPUT type="text" name="firstname" id="firstname"></TD>
<TD></TD><TD></TD>
<TD> </TD><TD> </TD>
</TR>
<TR>
<TD> </TD><TD> </TD>
<TD> </TD><TD></TD>
<TD> </TD><TD><input type="submit" value="Submit"></TD>
</TABLE>
</FORM>
The PHP code output starts as follows:
This is where we end up.
Using `$_POST["firstname"]` which outputs nothing.
Using `htmlspecialchars($_POST["firstname"])` which also outputs nothing.
Question:
The PHP output doesn't include the value that I entered into the field.
Can anyone see what I am doing incorrectly?
I see nothing wrong here, so I can only assume it is something wrong with how you output it on your PHB_action.php page.
You say that you're placing $_POST['firstname'] on your page, but have you actually made sure to echo or print it to the page?
You can do this like so:
echo $firstname = $_POST['firstname']; // notice the echo placed before
or
$firstname = $_POST['firstname'];
print("$firstname");
EDIT:
I've notice you have put your post data inside of single quotation marks when echoing out to your page.
You must concatenate on your data rather than putting them inside of single quotes when echoing, like so:
echo 'Using' . $_POST['firstname']; // notice the dot in between the string and the post data.
Either that, or you have not installed PHP correctly (or at all) onto your server.
Hope this helps
So, this is pretty straight forward and I have written it up and will explain each bit as i go.
The PHP you need for this is:
<?php
if (isset($_POST['send']))
{
$fname = $_POST['firstName'];
if (!empty($fname))
{
echo "hello $fname";
} else {
echo "Please supply your first name.";
}
}
?>
$_POST['send'] is the name of your submit button, this will be the trigger for your PHP to initiate and run through the rest of the code.
$fname = $_POST['firstName']
This is just where I prefer to store the $_POST as a variable in the event you are going to re use it again it saves time writing the entire thing.
if(!empty)
if the username isn't empty (!empty meaning not empty) then perform the echo of $fname. however if it comes back as empty it will echo the else echo "please supply...;
Now for the form.
<form action="" method="post">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td><input type="submit" name="send"></td>
</tr>
</table>
</form>
Just a straight forward form with a blank action on mine (I prefer to keep the PHP within the same file however I normally relay it back to a Class within a different file.
Each form input (First Name / Submit) must have a name="" value otherwise the PHP cannot read it and run with it.
I hope this makes sense and isn't too puzzling :)
Your input field should be inside tag and method should be post. Like:
<html>
<body>
<Form method=post>
<input id=mytextfield name=mytextfield type=text />
<input type=submit value=Submit />
</Form>
</body>
</html>

Posting form to a php script using target attribute to an iframe

I am working on a php/HTML web project and I have a form which contains an input type file which should be posted to a PHP script to upload an image onto the server. In the form tag I am using the target attribute to target an iframe so that it can simulate an AJAX style post, i.e. it posts to the iframe so the iframe does the work without requiring the main page to refresh.
Below is the form that I am using.
<form method="post" action="phpHandler/image-uploader.php" target="image_upload_frame">
<table>
<tr>
<td>
Image Title:
</td>
<td>
<input type="text" id="txtImageTitle" name="txtImageTitle" />
</td>
</tr>
<tr>
<td>
Image File:
</td>
<td>
<input type="file" id="txtImageFile" name="txtImageFile" />
</td>
</tr>
</table>
Below is the iframe
<iframe id="image_upload_frame" name="image_upload_frame"></iframe>
Below is the PHP script that the form is supposed to post to.
echo "posting";
$title = $_POST['txtImageTitle'];
$tempName = $_FILES['txtImageFile']['name'];
writeToLog("Image Title:" . $title);
writeToLog("Temp Name: $tempName\n");
$data = var_export($_POST, true);
$fileData = var_export($_FILES, true);
writeToLog($data);
writeToLog($fileData);
function writeToLog($message)
{
$fh = fopen('log.txt', 'a');
fwrite($fh, $message);
fclose($fh);
}
The PHP script is being exeucted but the $_FILES and $_POST arrays are empty.
I've tried changing the iframe to contain the src attribute pointing to the php script and removed the action from the form but this doesn't seem to even execute the PHP script.
Thanks for any help you can provide.
add
enctype="multipart/form-data"
to
<form method="post" action="phpHandler/image-uploader.php" target="image_upload_frame">

Javascript PHP a pop up image appear when click on search, after close image display search results

I want to do a 'pop up image' using javascript to display a simple user guide image, after the user click on search the image will pop up, to give them a better understanding of the results. So when the user close the image they will be link to results.php. Sorry that I can't provide any useful javascript codes, because those codes that I've found from the internet is too long. And I'm very noob at javascript.
Save Post->Session
<?php
$loan_amt = $_POST['loan_amt'];
if($_POST['search']){
if($_POST['loan_amt']=="" || $_POST['loan_tenure']==""){
$error = "Please fill up the mandatory fields";
}else{
session_start();
$_SESSION['property_type'] = $_POST['property_type'];
$_SESSION['property_status'] = $_POST['property_status'];
$_SESSION['loan_amt'] = $_POST['loan_amt'];
$_SESSION['loan_tenure'] = $_POST['loan_tenure'];
header("location:rates_result.php");
}
}
?>
Search Form (loan amount field)
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="this.loan_amt.value=this.loan_amt.value.replace(/\,/g,'')">
<table width="400px">
<tr>
<td class="color1" width="130">Loan Amt (SGD)*:</td>
<td width="258" align="left">
<input type="text" style="width:150px;font-size:16px" onkeyup="format(this)" onchange="format(this)"
onblur="if(this.value.indexOf('.')==-1)this.value=this.value" name="loan_amt">
</td>
<td width="258" align="left"><input type="submit" class="buttonStyle" name="search" value="search" /></td>
</tr>
</table>
</form>
in the popup page you can put in section:
<script type="text/javascript">
window.onclose=goToResults
function goToResults(){
//do the parent go to results.php
parent.location="results.php";
}
</script>
if its a form to be submitted you can point a function on parent and that function can submit the form:
in popup:
function goToResults(){
//do the parent go to results.php
parent.submitForm();
}
in page:
function sumbitForm(){
form_to_submit=document.getElementById("FORM_ID");
form_to_submit.submit();
}

SQL Query not updating data

I wrote a simple form from which a user will change his/her name , Facebook Name and image
here is the profile.php code with the form
<!!--edit form--!!>
<div id="edit">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td>
<table width="100%" border="0" cellpadding="1" cellspacing="1"bgcolor="#FFFFFF">
<tr>
<form method="POST" action="save_profile.php">
<td colspan="3"><strong>Username<br><? echo $row['session'];?></strong></td>
<td colspan="3"><strong>Name</strong><input type="text" name="name" id="name"
value="<? echo $row['name'];?>"/><br></td>
<td colspan="3"><strong>Facebook</strong><input type="text" name="fb" id="fb" value="<? echo $row['facebook'];?>"/></td>
<td colspan="3"><strong>Image</strong><input type="text" name="img" id="img" value="<? echo $row['img'];?>"/></td>
<input type="hidden" name="pros" />
<input type="submit" value="Save" />
</form>
and this is the save_profile.php
<?
include"sp-includes/sp-config2.php";
$resultz = mysql_query($slctq);
while($rowqw = mysql_fetch_array($resultz, MYSQL_ASSOC))
{
if($_POST['pros']){
$name=$_POST['name'];
$fb=$_POST['fb'];
$img=$_POST['img'];
$do =mysql_query("UPDATE profile SET name='$name', facebook='$fb', img='$img' WHERE id='$rowqw[id]'");
}
echo $rowqw['id'];
}
?>
I dont Know where i am wrong..
First of all, PLEASE SANITIZE YOUR QUERIES. Your query is completely open for exploitation right now and that might entirely be the reason why it fails.
Write your query like this:
mysql_query('UPDATE profile SET name="'.mysql_real_escape_string($name).'", facebook="'.mysql_real_escape_string($fb).'", img="'.mysql_real_escape_string($img).'" WHERE id="'.mysql_real_escape_string($rowqw['id']).'";');
Also, note that the rowqw index should be written as 'id' instead of id.
The problems with your code:
You are not checking for errors. Use mysql_error().
You are not checking your input (if it's valid or not). You should be binding parameters or escaping with mysql_real_escape_string.
Put the query in a separate string. Something like $query = "UPDATE ..."; $do = mysql_query($query);. It is useful for debugging. You know what the exact query you are sending is.
You are using $rowq[id] the wrong way. When in a string you either use the . notation, you concatenate multiple strings; or you enclose it in {$rowq[id]}.
When you do all this, you'll solve the problems yourself. Read the docs too.
Change the code to
$do = mysql_query("UPDATE profile SET name = '$name', facebook = '$fb', img = '$img' WHERE id = '$rowqw[id]'");

Why POST and GET method Form Not Working on PHP?

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?
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.
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.
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 tag should be like : <input type="value" name="value" />
Form tag should be like : <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.

Categories