PHP - Not able to get Text field value - php

I am new to PHP, I have mentioned below my HTML and PHP codes. I am not able to get the textbox value to PHP. I have mentioned below both HTML and PHP codes.
HTML Code:
Section <input type="text" name="f_b1_s1_sec" id="f_b1_s1_sec">
Content <input type="text" name="f_b1_s1_con" id="f_b1_s1_con"><br><br>
Para <input type="text" name="f_b1_s1_para" id="f_b1_s1_para">
PHP Code:
$tx0 = $_POST["f_b1_s1_sec"];
$tx1 = $_POST["f_b1_s1_con"];
$tx2 = $_POST["f_b1_s1_para"];
Please Clarify.
Thank you!

Try this:
<?php
if( isset( $_POST["go"] ) {
$tx0 = $_POST["f_b1_s1_sec"];
$tx1 = $_POST["f_b1_s1_con"];
$tx2 = $_POST["f_b1_s1_para"];
}
?>
<form action="insertyourphp.php" method="post">
Section <input type="text" name="f_b1_s1_sec" id="f_b1_s1_sec">
Content <input type="text" name="f_b1_s1_con" id="f_b1_s1_con"><br><br>
Para <input type="text" name="f_b1_s1_para" id="f_b1_s1_para">
<input type="submit" name="go" value="Go">
</form>

Related

PHP won't display Echo statement

I'm having trouble checking as to why my echo statement does not appear. I am using a web hosting service so I can use PHP scripts (double checked using simple echo "Hello World!" PHP scripts before). My file does have a .php file extension.
My goal is to simply add two numbers using PHP function from two inputs and just display the result.
<body>
<div>
<form action="index.php">
Enter First Number:<br>
<input type="text" name="first_input" value="">
<br>
Enter Second Number:<br>
<input type="text" name="second_input" value=""><br>
<input type="submit" value="Calculate">
</form>
<?php
function Calc_Addition() {
$first_input = filter_input(INPUT_GET, 'first_input');
$second_input = filter_input(INPUT_GET, 'second_input');
$amount_output = $first_input + $second_input;
echo "$amount_output";
}
?>
</div>
</body>
You need to both parse the input values as numbers (or pass them as such) and execute the function you're calling, like so:
<body>
<div>
<form action="index.php" method="get">
Enter First Number:<br>
<input type="text" name="first_input" value="">
<br>
Enter Second Number:<br>
<input type="text" name="second_input" value=""><br>
<input type="submit" value="Calculate">
</form>
<?php
function Calc_Addition() {
$first_input = intval($_GET['first_input']);
$second_input = intval($_GET['second_input']);
$amount_output = $first_input + $second_input;
echo $amount_output;
}
if(isset($_GET['first_input'])) {
Calc_Addition();
}
?>
</div>
</body>
You need to execute the function. You aren't doing so
<body>
<div>
<form action="index.php">
Enter First Number:<br>
<input type="text" name="first_input" value="">
<br>
Enter Second Number:<br>
<input type="text" name="second_input" value=""><br>
<input type="submit" value="Calculate">
</form>
<?php
//Check if isset, then execute function
if (isset($_GET['first_input'])) {
Calc_Addition();
}
function Calc_Addition() {
$first_input = filter_input(INPUT_GET, 'first_input');
$second_input = filter_input(INPUT_GET, 'second_input');
$amount_output = $first_input + $second_input;
echo "$amount_output";
}
?>
</div>
</body>
I think you need to cast your $first_input and $second_input variables as int's before adding them. So $amount_output would read as follows:
$amount_output = intval($first_input) + intval($second_input);

PHP simple form not posting

I've been tearing my hair out trying to figure out why the isset($_POST['Submit']) is not executing with my form. The data from the form is just not passing into the php code. Basically the code does not seem to be recognizing something like $ffname = $_POST["ffname"];
<?php
$ffname = $flname = $femail = $fcemail = $fpass = $fcpass = "";
if(isset($_POST['ffname'])){
$ffname = $_POST["ffname"];
$flname = $_POST["flname"];
$femail = $_POST["femail"];
$fcemail = $_POST["fcemail"];
$fpass = $_POST["fpass"];
$fcpass = $_POST["fcpass"];
echo "<p>Hello World<p>";
$con = mysqli_connect("localhost", "root", "") or die(mysqli_error());
mysqli_select_db($con, "userdata") or die(mysqli_error($con));
mysqli_query($con,"INSERT INTO tbluser (fname, lname, email, pass) VALUES('$ffname', '$flname', '$femail', '$fpass')")
or die (mysqli_error($con));
}
?>
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
The other answer by #DerVO is correct. But there seems to be something else at play, since you say it still doesn't work.
A comment became too long, so I've built a full answer here.
Step 1:
Add a name to your input:
<input type="submit" name="Submit" value="submit">
However, relying on the submit in your $_POST is not the best plan. So I suggest watching a different form field - for example, ffname:
Step 2:
Improve your watch, using a different field:
if ( isset( $_POST['ffname'] ) ) {
// do your work
}
Lastly, you may be munging your form action attribute.
Step 3:
In order to keep things simple, if the form is supposed to submit to the same page, you can simply omit the form action.
<form method="post">
Betweeen these three items, the form will work, unless you have some problem with your server.
Step 4:
Clean up your form formatting. You've got odd spacing which is problematic. In an html element, the property="value" code needs to be without spaces, but spaces between properties. Example:
<!-- Your version -->
<input type = "text"name = "ffname"id = "ffname"value="<?php echo $ffname;?>"><br>
<!-- Clean / correct version -->
<input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Here's a "clean" version of your whole form:
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
You need to give your input submit a name:
<input type="submit" name="Submit" value="Submit">
You have pass name of element in $_POST
try put name attribute in input submit
<input type = "submit" name="Submit" value = "1">

Control elements not by name with COM “InternetExplorer.Application” object

I have a example page (http://**.com/def.php") code is:
<form action="abc.php" method="POST" name="f_action">
<input type="text" name="id">
<input type="password" name="pwd">
<input type="submit" value="OK" name="action">
</form>
and I used php with COM “InternetExplorer.Application” object to fill and submit the form:
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->HWND;
$Browser->Visible = true;
$f1_url = "http://**.com/def.php";
$Browser->Navigate($f1_url);
sleep(5);
$Browser->Document->f_action->id->focus();
$Browser->Document->f_action->id->value = $p_user_name;
$Browser->Document->f_action->pwd->focus();
$Browser->Document->f_action->pwd->value = $p_user_pwd;
$Browser->Document->f_action->action->focus();
$Browser->Document->f_action->action->click();
It can work and easy to use by name attribute!
BUT if I change the part of example page code to this:
<form action="abc.php" method="POST">
Change "name='f_action'" to "NO name attribute", How can I do to fill and submit the form with COM “InternetExplorer.Application” object and php?
Thank you and sorry for my poor English.

HTML submit forms using PHP

I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];

PHP output in the textfield/text

i have a little problem about PHP
there is two input for num1 and num2
and another input answer,,can the output in the php be putted in the input text answer??
<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer">
Try something like this:
<?php
if(!empty($_POST['num1'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2']
$answer = $num1 + $num2;
}
?>
<form action="yourpage.php" method="post">
<input type="text" name ="num1" value="<?=#$num1?>">
<input type="text" name ="num2" value="<?=#$num2?>">
<input type="text" name ="answer" value="<?=#$answer?>">
<br/>
<input type="submit" id="btnSubmit" value="POST ME" />
</form>
Not tested btw...
<?php
//get the values and calc..
$answer = $_GET['num1'] + $_GET['num2'];
//more secure, $answer = floor($_GET['num1']) + ...
?>
<html><body>
<form method="get" action=".">
<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer" value="<?php echo $answer;?>">
<input type="submit">
</form>
...
The jQuery answer that was suggested by riky has shown up on SO before. Try:
How to update the value in one text box based on the value entered in another text box?
As per the OP's request for an fleshed out version of the jQuery answer, modifying the linked to answer gives something like:
$('#num1').change(function() {
var txtAmtval = $('#num1').val() + $('#num2').val();
$('#answer').val(txtAmtval);
});
$('#num2').change(function() {
var txtAmtval = $('#num1').val() + $('#num2').val();
$('#answer').val(txtAmtval);
});
Is that what you had in mind?
Obviously you'd need to include jQuery too.
Please see code in action here: http://jsfiddle.net/9KEsY/

Categories