I need a solution of php post method, I have solution for get method.
Example:
Folder: /vaibrother
1st file source: data_get.php
<?php
$data="nasir=90,sajib=80,masum=100,yeasin=110,mayeen=99";
$data=explode(",",$data);
for($i=0; $i<count($data); $i++){
$ns=explode("=",$data[$i]);
$name=$ns[0]; $score=$ns[1];
if(isset($_GET["id"]) && $_GET["id"] == $name) $output = "$name = $score";
} if(!empty($output)) echo $output; ?>
<hr>
<form action="" method="GET">
<input name="id" value="masum"> {example: nasir, sajib, masum, yeasin, mayeen}
<input type="submit" value="check">
</form>
2nd file source: data_post.php
<?php
$data="nasir=90,sajib=80,masum=100,yeasin=110,mayeen=99";
$data=explode(",",$data);
for($i=0; $i<count($data); $i++){
$ns=explode("=",$data[$i]);
$name=$ns[0]; $score=$ns[1];
if(isset($_POST["id"]) && $_POST["id"] == $name) $output = "$name = $score";
} if(!empty($output)) echo $output; ?>
<hr>
<form action="" method="POST">
<input name="id" value="masum"> {example: nasir, sajib, masum, yeasin, mayeen}
<input type="submit" value="check">
</form>
3rd file source: get_success.php
This is a proxy<hr>
<?php
$fp=fopen("http://localhost/vaibrother/data_get.php?id=mayeen","r");
echo fread($fp,99999);
fclose($fp);
?>
4th file source: post_success.php {??????}
(I need the solution of this file)
This is a proxy<hr>
<?php
/*
http://localhost/vaibrother/data_post.php
id = mayeen
[ How to display result 99 ? ]
I Dont Know
*/
?>
You cannot send data as POST using PHP. A workaround could be found here.
Some ways include using a FORM to send POST data to a specific PHP file. Example:
<html>
<body>
<form name="POSTFORM" method="post" action="http://localhost/vaibrother/data_post.php">
<input type="text" name="id" value="mayeen">
<input type="submit" value="Submit">
</form>
</body>
</html>
Your post method should be define like
<div class="form-group">
<label for="form_name">Firstname *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
</div>
</div>
and then get the post value like
Welcome
Related
I am trying to make a button that saves form data on a file and redirects you afterwards, but although the save function works just fine, the redirection doesn't happen. I've tried only href and formaction so far. Any suggestions?
Paste bin : pastebin
Thank you for your time reading this !
the code:
<html>
<head></head>
<footer>
<form method="post">
<input type="radio" id="html" name="fav_language" value="private_number" checked="checked">
<label for="html">private_number</label><br>
<input type="radio" id="css" name="fav_language" value="clieant_number">
<label for="css">clieant_number</label><br>
<input type="text" id="fname" name="fname" maxlength="11" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" required >
<button type="submit" name="BUY"><i class="arrow right"></i></button>
</form>
</footer>
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
}
?>
</body>
</html>
You can use PHP's header() function for this. Change the PHP part to this
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
?>
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
I have an index.php and output.php
I need two buttons both linked to output.php but to different functions and I can't get it work. Here is my code:
In index.php:
<?php
include 'output.php';
if (isset ($_POST ['choice3'])){
choice3();
}
if (isset ($_POST ['choice4'])){
choice4();
}
?>
<form method="post" action="output.php">
<input type="submit" name="choice3" id="choice3" value="Choice 3">
<input type="submit" name="choice4" id="choice4" value="Choice 4">
</form>
In Output.php,
function choice3(){} and function choice4(){}
Each function have different output.
But now, when I click the button, nothing shows up.
Thanks for help!!
Replace your index.html.php and output.php with this
Index.php
<form method="post" action="output.php">
<input type="submit" name="choice3" id="choice3" value="Choice 3">
<input type="submit" name="choice4" id="choice4" value="Choice 4">
</form>
Output.php
<?php
if (isset ($_POST ['choice3']))
{
/ body of the choice 3 function without the function signature
}
elseif(isset ($_POST ['choice4']))
{
/ body of the choice 4 function without the function signature
}
?>
Just Replace your code with this
<?php
include 'output.php';
$choice = $_POST['choice'];
if ($choice == 'choice3') {
choice3();
} else if ($choice == 'choice4') {
choice4();
}
?>
<script>
function make_choice(choice) {
document.getElementById('choice').value = choice;
document.choice_form.submit();
}
</script>
<form method="post" action="" name="choice_form">
<input type="hidden" name="choice" id="choice" value="" />
<input type="button" name="choice3" id="choice3" value="choice3" onclick="make_choice(this.value);">
<input type="button" name="choice4" id="choice4" value="choice4" onclick="make_choice(this.value);">
</form>
I'm creating a page when user can enter how many files he want to upload then that page is rendered again with a form, after submission to another page the files are uploaded.
But i'm receiving an error [if i upload 1 file, else the problem is userfile[limit-1]]
Notice: Undefined index: userfile0 in E:\wamp\www\uploader\uploader.php on line 10
I'm pasting here my code
index.php
<html>
<?php
if (isset($_POST['num']) && !empty($_POST['num']))
{
?>
<form name="uploader" action="uploader.php" method="post">
<table>
<tr><td>Title</td><td>Select File</td><td>Description</td></tr>
<input type="text" name="number" value="<?php echo $_POST['num']; ?>"/>
<?php
for ($i=0;$i<$_POST['num'];$i++)
echo '<tr><td><input type="text" name="title'.$i.'"/></td>
<td><input type="file" id="userfile'.$i.'" name="userfile'.$i.'" size="30"></td>
<td><textarea name="desc'.$i.'" rows="4"></textarea></td></tr>';
?>
</table>
<input type="submit" name="b" value="Submit"/>
</form>
<?php
}
else
{
?>
<form name="form" method="post">
How many files to upload ? <input type="text" name="num" value="1"/>
<input type="submit" value="Submit"/>
</form>
<?php
}
?>
</html>
uploader.php
<?php
if (isset($_POST['number']))
{
for ($slot = 0; $slot < $_POST['number']; $slot++)
{
$title = $_POST["title$slot"];
$desc = $_POST["desc$slot"];
if (move_uploaded_file($_FILES["userfile$slot"]['tmp_name'],$_FILES["userfile$slot"]['name']))
echo $name.' file Uploaded !';
else
echo ' file not Uploaded ! ';
}
}
?>
Edit
SQL code removed
You need an enctype="multipart/form-data" on your <form> tag, otherwise it cannot upload files/PHP cannot read it.
You should also check the existance of a file before trying to upload it.
if (isset($_FILES['userfile'.$slot])) ...