I don't know how to do it but hope anyone could help me about my program.
My program flows like this.
I have search.php - it will search unique id. I also have a button here, once submitted/loaded, it will echo all of the data on my index.php
index.php - this is where all data will be loaded. my goal is to update the fields. What i wanted to do is that test.php (contains my condition) will be read only after user click submits. because i tried to include it and place it on the top but it gives me error and not giving me the data that supposedly to be loaded. but when i tried to removed it, all data is successfully echoed on the textbox.
My test.php should be trigger once it was submitted.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox 1: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Textbox 2: <input type="text" name="txt1" value="<?php echo $txt2;?>">
Textbox 3: <input type="text" name="txt1" value="<?php echo $txt3;?>">
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$txt2 = "";
$txt3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$txt1 = $_POST["txt2"];
$txt1 = $_POST["txt3"];
}
?>
If I understand it correctly, this should be what you want:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
include 'test.php';
}
?>
instead of
<?php include 'test.php' ?>
I would do this with Javascript and Ajax, but what you should first do, is give your input-elements unique names (now its txt1 in all of them) as well as the variables in test.php
you missed ; at the end of include statement.
<?php include 'test.php';?>
Thank you ....
Related
I like to store an input of a HTML form into a PHP variable:
HTML Form:
<form action="shell.php" method="post">
<p>Youtube Link: <input type="text" name="url" /></p>
<p><input type="submit" /></p>
</form>
PHP File (shell.php)
<?php
value=<?php echo ($_POST['url']); ?>;
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
?>
How I can store the input of the HTML form into the php variable $value?
Thanks for your help, best regards
You don't need to open another PHP tag. Replace your code to below:
<?php
$value = $_POST['url'];
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
?>
And if you want to verify the value is not empty:
<?php
if (isset($_POST['url'])) {
$value = $_POST['url'];
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
}
?>
You don't need to modify the file itself. You just can pass the input VALUE to the required expression:
Check this:
<?php
if( !empty($_POST['url']) )
exec('/bin/bash /home/d_youtube/youtube.sh "' . $_POST['url'] . '"');
?>
I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];
I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding
I managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?
Here's my code:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
?>
<script type="text/javascript">
var val1 = document.getElementById('val').value;
</script>
<?php
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
<form method="post" action="">
Enter value: <input type="text" name="value" id="val" /><br/>
<input type="submit" value="Submit" name="submit" />
</form>
</html>
Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)
As far as I understand your question, what you need to do is:
<?php
//Catches the value of the Submit button:
$submit = isset($_POST['submit']);
if($submit) {
$val1 = $_POST['val']; // TO store form passed value in "PHP" variable.
/* The below $res contains the URL where I wanna append the caught value.
Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or
what the user searches for)&for=trackname&agent=agent
*/
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
} // This is to end the "if" statement
?>
and then change the form input field to:
Enter value: <input type="text" name="val" id="val" /><br/>
I am not sure if POST accepts id values too!
Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.
Then change your PHP code to this:
if(isset($_POST['value'])) {
$res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}
I am just starting to learn php, how would I initiate a echo statement after a submit button is pushed, or even a anchor tag.
Here is my code so far
form name="myform" method="get" actions="madlib01.php"
Name: <input type="text" name="name" /> <br />
<input type="submit" name="submit" />
form
<?php
$Name = $_GET['name'];
$hello .= "Hello $Name";
echo $hello //I would prefer the echo to happen after the submit button is hit
?>
the correct attribute for your form tag is "action", not "actions"
When the form is submitted, a new request is sent to the server (in your case, using GET).
So to do it all in one page:
form.php:
<form action="form.php" method="GET">
<input type="text" name="name"/>
<input type="submit">
</form>
<?PHP
if (! empty($_GET['name'])){
echo 'Hello, ' . $_GET['name'];
}
?>
You will first need to check if PHP has received your GET parameter using isset or array_key_exists:
if(isset($_GET['name']) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
}
or:
if(array_key_exists('name', $_GET) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
} else {
//example: default to something if nothing has been passed
echo "Hello Guest";
}
Also note, if you're submitting to the same page, you can omit the action attribute from your form tag altogether:
<form method="GET">
echo $hello
You've just gained an HTML-injection vulnerability. If someone sends your user to:
http://www.example.com/madlib01.php?name=<script>stealYourCookies()</script>
you've got problems.
Yes, this is a My First PHP Script. That doesn't make security optional. This is a mistake every tutorial makes: teaching bad practice from the start, treating correctness (and security, which is a subset of correctness) as an optional extra.
The result is that most PHP code out there is full of holes. But there's no need for yours to be! Every time you place a pure-text string into a surrounding HTML context, escape it properly:
echo htmlspecialchars($hello);
I tend to define a function with a shorter name than ‘htmlspecialchars’ to do that for me, as I'm lazy.
<?php
function h($text) {
echo(htmlspecialchars($text, ENT_QUOTES));
}
$name= '';
if (isset($_REQUEST['name']))
$name= trim($_REQUEST['name']);
?>
...
<?php if ($name!=='') { ?>
<p> Hello, <?php h($name); ?>! </p>
<?php } ?>
<form method="get" action="madlib01.php">
<p>
<label for="namefield">Name:</label>
<input id="namefield" type="text" name="name" />
</p>
<p>
<input type="submit" />
</p>
</form>
Now if you say your name is Mister <script>, the page will greet you exactly as such, angle brackets and all, instead of trying to run JavaScript. This is the correct output and thus also secure.