Get value from HTML input form with PHP [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am wanting to literally take the HTML form input with the ID "bet" and after the value has been entered and submitted; get the exact input for example: '100' may be entered, as I am wanting to perform an if statement to check that the value submitted isn't less than 0 for obvious reasons so then I can stop the webpage proceeding to perform the bet, and make the user instead enter a valid amount.
The code that I am having an issue with is below, upon loading the page I am getting the error: Notice: Undefined index: bet
<form action="duel.php" name="duel" id="duel">
<input type="text" id="betamount" name="betamount">
<?php
$data = $_GET['betamount'];
echo $data;
?>
</form>
I am fairly new to programming in PHP, so any help would be greatly appreciated.

You need to assign a name to the input element. In your situation, you could use the same name as your id:
<input id='bet' name='bet' type='text' value='100' />
To get the specific data for the 'bet' input field use:
echo $_POST['bet'];
On your server to view all of the post data use the code:
// Wrapping the output in the pre block makes the POST data easier to read
echo '<pre>';
print_r($_POST);
echo '</pre>';

This is an example script you can use:
php file:
<?php
if(isset($_POST['submit'];)) {
session_start();
$text = $_POST['Text'];
echo "$text";}else {echo 'Could not load text!';}
?>
<form method="POST">
<input name="Text" type="text">
<input type="submit" name="submit">
</form

So, you would make your form method "POST' and the action the url of the PHP script.
Then, in the PHP script you would use $_POST variable which would contain all of the info that was submitted in that form. See here:
http://php.net/manual/en/reserved.variables.post.php
There is a similar variable for get requests. For hte difference in get and post methods see here:
http://www.w3schools.com/tags/ref_httpmethods.asp

Related

How can I get the value of the checkbox in PHP? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I am trying to make a registration page for my website but when I run it; it gives error. What am I doing wrong?
<?php
//Form values
$name_value=$_POST['Name_input'];
$Username_value=$_POST['Username_input'];
$DOB_value=$_POST['DOB_input'];
$Password_value=$_POST['Password_input'];
$Phone_number_value=$_POST['Phone_number_input'];
$Python_checkbox_value=$_POST['Python_checkbox'];
$Java_checkbox_value=$_POST['Java_checkbox'];
$C_sharp_checkbox_value=$_POST['C#_checkbox'];
$HTML_checkbox_value=$_POST['HTML_checkbox'];
$Cpp_checkbox_value=$_POST['C++_checkbox'];
$R_checkbox_value=$_POST['R_checkbox'];
$swift_checkbox_value=$_POST['swift_checkbox'];
$kotlin_checkbox_value=$_POST['kotlin_checkbox'];
$JS_checkbox_value=$_POST['JS_checkbox'];
Take a look at this code :
<form method="GET">
<input type="checkbox" value="value" name="name">checkbox label</input>
<input type="submit">
</form>
<?php
if (isset($_GET["name"]))
{
echo $_GET["name"];
}
?>
As you can see when I submit the form without checking, the parameter in URI is empty because the checkbox isn't defined (I used GET method)
But when I check it, you can see the parameter is name=value

Fetch details from database using onclick() [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have simple HTML form on which user will enter token number. How can I fetch details from database using onclick() event when user enter token number and click on fetch details.
After fetching the details user will click on Pay with PayPal button to complete payment.
HTML Form:
<form class="responsive" action="payments.php" method="post" id="paypal1">
<strong><big>Have Token? Checkout here...</big></strong><br><br>
<p>Token Number:
<input type="text" name="token" placeholder="Enter Token Number" autocomplete="off" maxlength="45" required />
<input type="button" id="token-btn" value="Fetch Details" onClick="fetch-details()">
</p>
<button class="btn btn-success">Pay With PayPal</button>
</form>
The usual way is either:
Old-style, page-refresh: Have the button submit a form, have your PHP code do the DB work, and show a new page.
Use ajax:
Have a JavaScript event handler that makes an ajax call to your PHP code (using fetch [on modern browsers] or XMLHttpRequest)
Have your PHP code do the DB work and return the details, perhaps using json_encode to send the details back as JSON
Have your JavaScript ajax success handler use the details to update the page via DOM manipulation
Download jquery from jquery site include the script in the page needed, then create a php file lemme call mine fetch.php that fetches the details you want, something like this
<?php
require_once 'your_connection';
if(isset($_POST['id'])){
$id = mysqli_real_escape_string($connect, $_POST['id'];
$query = "SELECT * FROM `table` WHERE `id`='$id'";
if($query_run= mysqli_query($query)){
if($row = mysqli_fetch_assoc($query_run)){
echo"<h1>$row['details']</h1>";
}
}
}
?>
then below the included script of downloaded do this
$(document).ready(function(){
$("#fetchDetails").click(function(){
$.post("fetch.php",{id:the_id_of_the_details_you_want_to_fetch},function(data)
{
$("#id_of_where_you_want_to_place_details_on_page").html(data);
})
});
})
hope it helps

Stop refreshing on submit button [duplicate]

This question already has answers here:
JavaScript code to stop form submission
(14 answers)
Closed 4 years ago.
Is there any way to stop refreshing the page on submit button if your if condition goes false and show all the input fields with values entered?
Since you mentioned PHP, then why not use it?
I assume your question has two parts like below.
Part one - you wrote:
show all the input fields with values entered
By above, you mean using $_SESSION to repopulate the fields with the submitted data?
Part two - you wrote:
Is there any way to stop refreshing the page on submit button if your if condition goes false
Note that submit and any on is an event within the client side processing scope. You can use jQuery or JS validations for that.
Here below are two files for your learning test.
The posting php:
<html>
<body>
<form action="action.php" method="POST">
<input type="text" name="feedback" value="" />
<input type="hidden" name="token" value="123456ABCDEF" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
<?
session_start();
$token = '123456ABCDEF';
if(isset($_SESSION)){
if($_SESSION['token'] == $token){
echo "Your feedback:<br />";
foreach($_SESSION as $field=>$value):
echo $field.": ".$value."<br />";
endforeach;
}else{
echo " Bad token! Cross-Site Request Forgery (CSRF)";
}
}else{
echo "Nothing!";
}
The posted to php:
<?php
session_start();
$_SESSION = $_POST;
$_SESSION['message'] = "Thank you for the feedback!";
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Hope I got you right and above helps.
First off, this is a duplicate of JavaScript code to stop form submission
Now, when we got that out of the way, here is abrief explanation. You cannot prevent the browser itself from refreshing. But what you can do is prevent the form from being submitted (and in turn causing a refresh). And this is the usual solution when dealing with form validation in JS.
You can check the abode linked answer for more details.
You can use Post / Redirect / Get Pattern. If you have errors redirect to same page with form and show errors. You can't just "stop" the form during POST.
Other way if you don't want "showing" any redirect you can use ajax request. Form wont "blink".
You can also use javascript for checking field onchange event of input elements.

if statement doesn't work on php/html/javascript. Always false [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having trouble with the following code.
Why when I test if the $date variable, the if statement the return is always true?
The goal of the code is check if the $date variable is empty when I load for the first time the page. If the page was loaded for the first time, the $date variable will return the current date, if I selected a date form the FORM, it will load the same page and the variable $date will be the selected date.
<form action="#" method="POST">
<p>Select date: <input type="text" id="datepicker" name="datepicker" style="display: inline !important; width: 100px;">
<input type="submit" value="Ok"></p>
<?php
//Variable to return the value of datepicker (calendar)
if (empty($date)) { //condition to check if the $date variable is empty, if empty select current date and send value to another php script
$date=date("Y-m-d")."%"; //select current date
$_SESSION['sending_date'] = $date;
} else{// if the value of date was selected, send variable value to another php script
$_SESSION['sending_date'] = $_POST['datepicker'] . "%";
}
?>
<div id="draw_chart"></div>
</form>
I think you are trying to set $_SESSION['sending_date'] if $_POST['datepicker'] is empty.
Based on that assumption, the revised if statement should be: if (empty($_POST['datepicker']))

How to go to another html page and click a button automatically? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got two html page A and B. When I click a link in A, I want to open B and pass a value to B. Then this value will be added into a text input in B and a button in B will be click automatically?
How to achieve this function?
I´m not quite sure of what you want but i'll give it a try,
First you need JavaScript or some JS Library to achieve that and PHP.
First things First:
Create a link:
Go To Page B
Now you can pass a value using the GET method, since you want to pass a value you will need that the page format is .PHP and not .HTML. Then you add some value to your link:
Go To Page B
So you will have a value named "data" with '123' in it.
In page B you will need to open PHP initializer like this:
<?php
$data = $_GET['data'];
?>
<input type='text' value='<?=$data?>' />
<input type='submit'id="buttonToBeClicked" >
<script>
document.getElementById("buttonToBeClicked").click();
</script>
In this case you will need a form with a action. The stuff inside the will click the button automatcly
Hope it helped.
Use a form on page A, and then you will need some sort of server-side scripting language for pageB, such as PHP. In fact, if you want to, you can use the PHP file extension for both pages. PHP includes HTML.
PAGEA.PHP (or PAGEA.HTML)
<form id="myform" action="PAGEB.PHP" method="POST">
First Name:<br />
<input type="text" name="fname" />
<input type="submit" value="Submit Me">
</form>
PAGEB.PHP
<?php
$fn = $_POST['fname'];
?>
<html>
<body>
Received this name: <?php echo $fn; ?> <br />
<br />
That's right, I said [<?php echo $fn; ?>]<br />
In pure JavaScript with no server side:
A.html
go to B
B.html
<input type="text" id="my_text" />
<script type="text/javascript">document.getElementById('my_text') = document.location.hash.substr(1);</script>

Categories