How can I have two different _GET? (php, mysql, apache, phpmyadmin) - php

<?php
$mysql_pekare=mysqli_connect("localhost", "1","2", "3") or die(mysqli_error($mysql_pekare));
if(!empty($_GET['name'])) {
$query = "INSERT INTO Personinfo(`Personname`, `Personage`) VALUES('$_GET[namn]', '$_GET[age]')";
if (!mysqli_query($mysql_pekare,$query)) {
die('Error: ' . mysqli_error($mysql_pekare));
}
echo "Welcome ". $_GET["namn"];
}
?>
<form id="Personinfo" action="index.php" > <!-- default form method is GET -->
<input type="text" id="namn" name="namn" placeholder="namn"/>
<input type="text" id="age" name="age" placeholder="age"/>
<input type="submit"/>
</form>
</body>
<body>
<?php
$mysql_pekare=mysqli_connect("localhost", "1","2", "3") or die(mysqli_error($mysql_pekare));
if(!empty($_GET['Product'])) {
$query = "INSERT INTO Produkter(`ProduktNamn`, `ProduktPris`) VALUES('$_GET[Product]', '$_GET[Price]')";
if (!mysqli_query($mysql_pekare,$query)) {
die('Error: ' . mysqli_error($mysql_pekare));
}
}
?>
<form id="Produkter" action="index.php" > <!-- default form method is GET -->
<input type="text" id="Product" name="Product" placeholder="Produkt" />
<input type="text" id="Price" name="Price" placeholder="Pris"/>
<input type="submit"/>
</form>

You have two forms with different input names, so you can check for these names, instead of generic $_GET:
if( isset( $_GET['namn'] ) )
{
(...)
}
elseif( isset( $_GET['Product'] ) )
{
(...)
}
If you want be more chic, you can identify different forms through an hidden <input> identifier:
<form id="Personinfo" action="index.php" >
<input type="hidden" name="formID" value="Personinfo"/>
(...)
<form id="Produkter" action="index.php" >
<input type="hidden" name="formID" value="Produkter"/>
(...)
and in you php code, check for this:
if( isset( $_GET['Personinfo'] ) )
{
(...)
}
elseif( isset( $_GET['Produkter'] ) )
{
(...)
}

Related

PHP send variable value external webpage

I'm currently trying to send the two variable values, $incorrect and $correct from a form.
I have tried to use $_POST[] to create this functionality, yet it doesn't work!
Could anyone help me when creating such a functionality ?
page1.php
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
<input type="submit" name="submitAnswers" value="Submit">
</form>
<?php
if(isset($_POST["submitAnswers"])){
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct ++; }
else { $incorrect ++; }
if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct ++; }
else { $incorrect ++; }
if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct ++; }
else { $incorrect ++; }
}
?>
And on the receiving end
page2.php
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
$correct = $_POST['correct'];
$incorrect = $_POST['incorrect'];
echo $correct."test ".$incorrect;
?>
</body>
</html>
Your first page should JUST show the questions, it will be like this.
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<!--input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" /-->
<input type="submit" name="submitAnswers" value="Submit">
</form>
When you hit the submit button, the answers are posted to testresult.php.
In this file, first checking the answers, then showing the result.
The page doesn't contain questions, so you need to include the php file too.
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
if(isset($_POST["submitAnswers"])){
include 'question.php'; #include the question array.
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct ++; }
else { $incorrect ++; }
if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct ++; }
else { $incorrect ++; }
if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct ++; }
else { $incorrect ++; }
echo $correct."test ".$incorrect;
}
?>
</body>
</html>
Where did those variables from from?
In your code you're calling two vars $array_answer and $question_num_array.
None of these two variables are set! Please fix this issue.
The Form action you've set to your testresult.php. So surely the form will carry all of its data to that page. But you are trying to get the data on the same page as if(isset($_POST["submitAnswers"])) where is the form; what actually should exist in that testresult.php page. So should be like this:
index.php or something.php
<form action="testresult.php" method="POST">
<label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
<input type="text" id="question1_answer" name="question1_answer" required><br><br>
<label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
<input type="text" id="question2_answer" name="question2_answer" required><br><br>
<label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
<input type="text" id="question3_answer" name="question3_answer" required><br><br>
<input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
<input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
<input type="submit" name="submitAnswers" value="Submit">
</form>
testresult.php
<?php
// questions array that is called in the form's page should be
called here too to get the questions and comparisons.
if (isset($_POST['submitAnswers'])) {
$correct = 0;
$incorrect = 0;
$qn1_ans = $_POST['question1_answer'];
$qn2_ans = $_POST['question2_answer'];
$qn3_ans = $_POST['question3_answer'];
if ($qn1_ans == $array_answer[$question_num_array[0]]) {
$correct++;
} else {
$incorrect++;
}
if ($qn2_ans == $array_answer[$question_num_array[1]]) {
$correct++;
} else {
$incorrect++;
}
if ($qn3_ans == $array_answer[$question_num_array[2]]) {
$correct++;
} else {
$incorrect++;
}
}
?>
<html>
<head>
<title>Results</title>
</head>
<body>
<?php
$correct = $_POST['correct'];
$incorrect = $_POST['incorrect'];
echo $correct . 'test ' . $incorrect;
?>
</body>
</html>

I am getting a really weird result on my php form output

Here is my code:
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_POST['postName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$age = $_POST['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_GET['getName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$age = $_GET['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
I have two forms. Both displaying the exact same thing, but one form using POST and one using GET.
I have gotten so close to finishing this off but now I have another small/weird issue.
The code technically works correctly, but here's the output explanation:
when I first open up the page the GET form already has the result "Sorry, try again when you're 16 or older." When I fill out the first 'simple' form, it displays the result correctly but then the POST form shows the "Sorry, try again..." result. Then, when I fill in the information and click submit, it displays the correct result and the other two forms are blank as they're supposed to be, and then the same result when I fill out the GET form.
Any help on this is much appreciated.
Try this code :
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
if (isset($_POST['firstName']) && $_POST['lastName'])
{
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
}
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_POST['postName']))
{
echo $_POST['postName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset($_POST['age']))
{
$age = $_POST['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_GET['getName']))
{
echo $_GET['getName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
if (isset($_GET['age']))
{
$age = $_GET ['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
Please try this. I hope it will help.
Replace
if ($_SERVER['REQUEST_METHOD'] == "POST") {
with
if (isset($_POST['age'])) {
Similarly,Replace
if ($_SERVER['REQUEST_METHOD'] == "GET") {
with
if (isset($_GET['age'])) {
When you first enter on page, default REQUEST_METHOD is GET so you should check if isset($_GET['age']) {
and here check if it is more than 16
}
also you should check this one
echo $_GET['getName']; and change on this
echo isset($_GET['getName']) ? $_GET['name'] : "";
You should also check $_POST request like this and your program will work correctly.

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

Keep Form Data after php validation (POST method)

I'm validating my form with php and displaying error messsages but after every submit , the form is refreshed and the inputs are blank again . I was wondering if there was a way i could re-insert the user past data into the fields again .
I'm using POST method .
<form id="registration-form" action="" method="post">
<input type="text" name="username" id="username" placeholder="Username">
<button type="submit" name="_submit" id="submit" class="button-submit">Submit</button>
</form>
PHP function :
if( !isset( $_POST['_submit'] ) ){
return array( $array, $arrayexport );
}
$username = $_POST['username'];
// test function for username
if ( ($valid_username ) != 0 ) {
array_push($array, "Input Required"); }
else{
header( 'Location: /thankyou.php' );
}
YOu can do something as
<input type="text" name="username"
id="username" placeholder="Username"
value="<?php if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}?>">
made a cleaner version .
Thanks to Abhik Chakraborty
<input type="text" name="username" id="username" placeholder="Username" value="<?php user() ?>">
External php file :
function user(){
if(isset($_POST['username'])){ echo $_POST['username'];}else{ echo '';}}

Form Only Posting In 1 Column

Can someone explain or show me why my form is only posting in 1 column when it should be posting in 2 columns for example.
Here is my form
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="action" value="user_details" />
<input type="submit" class="submit_form_button" value="Update Details" id="details_change">
</form>
And here is my PHP
if (isset($action) && $action=='user_details' && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['about_me']) && isset($_POST['about_me'])) {
$_POST['$about_me'] = preg_replace("/[^a-z]/i","",$_POST['about_me']);
} else {
$about_me = '';
}
if (isset($_POST['profile_message']) && isset($_POST['profile_message'])) {
$_POST['$profile_message'] = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}
And I have columns in my user table called about_me and profile_message
And what happens is it will only post about_me and not profile_message any reason why?
You need to use $_POST['user_details'] and $_POST['profile_message']
In HTML:
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="user_details" value="" />
<input type="submit" class="submit_form_button" value="Update Details" name="action" >
</form>
PHP:
if (isset($_POST['action']) && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['profile_message'])) {
$profile_message = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
if (isset($_POST['user_details']) && isset($_POST['user_details'])) {
$user_details = preg_replace("/[^a-z]/i","",$_POST['user_details']);
} else {
$user_details = '';
}
$about_me = $_POST['about_me'];
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}

Categories