submit button being created inside of submit button - php

I'm having a problem with a submit button in a form, when clicked it first reloads the current page, which creates a submit button inside of the original one before posting to the post page.
You can see the behavior example here:
http://tampary.com/pick_game.php?c_id=15&p_id=0&f_id=1&l_id=1
You can click back in your browser after clicking "start game" and you will see the nested controls. Using Chrome and Firefox, you get same results. Any ideas what could be causing this behavior? ideally, I just want the php page to be redirected to after the form submit to just load up cleanly. Any help would be greatly appreciated
The complete code is as follows:
<?php
require_once(dirname(__FILE__).'/grl-lib/db.php');
require_once(dirname(__FILE__).'/grl-lib/ui.php');
require_once(dirname(__FILE__).'/grl-lib/family.php');
require_once(dirname(__FILE__).'/grl-lib/location.php');
require_once(dirname(__FILE__).'/grl-lib/contact.php');
$ui = new x_ui;
$ui->set_ui(1);
if (!empty($_POST)){
$p_id = $_POST['p_id'];
$f_id = $_POST['f_id'];
$l_id = $_POST['l_id'];
$c_id = $_POST['c_id'];
}else{
$p_id = $_GET['p_id'];
$f_id = $_GET['f_id'];
$l_id = $_GET['l_id'];
$c_id = $_GET['c_id'];
}
?>
<!DOCTYPE HTML>
<html lang="<?php echo $ui->getLocale($ui->language_id,1); ?>">
<head>
<?php include_once('grl-lib/gen_include.php'); ?>
<meta name="robots" content="noindex"> <!-- no indexing this page -->
<link href="css/main.css" rel="stylesheet" type="text/css">
<link href="css/devices.css" rel="stylesheet" type="text/css">
<title>Tampary<?php ' '._('game'); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name=viewport content="width=device-width, initial-scale=1">
</head>
<body class="color_bg">
<?php include('standard/header.txt'); ?>
<!-- all content -->
<div class="entire_content_length aling_text_c">
<div class="max_width_responsive align_container_c spc_a_5">
<div class="align_container_c spc_a_5 container_c">
<form id="pick_game" action="game.php" method="post">
<?php
$html = '<input type="hidden" name="p_id" id="p_id" value="'.$p_id.'"><input type="hidden" name="f_id" id="f_id" value="'.$f_id.'"><input type="hidden" name="l_id" id="l_id" value="'.$l_id.'"><input type="hidden" name="c_id" id="c_id" value="'.$c_id.'">';
//$html = $html.'<input type="submit" value="'._('Start Game').'">';
echo $html;
?>
<input type="submit" value="Start Game">
</form>
</div>
</div>
</div>
<?php include('standard/footer.txt'); ?>
</body>
</html>

I see you are using an Ajax script, which is fine. But is definitely the culprit cause I can see it responding the entire page in firebug.
I will bet you that your front page is Ajax to and you append something somewhere (can't find it this fast).
Whatever the case, I'm not convinced that what ever your doing (loading an entire page through AJAX) is good practice.
Looking at your AJAX requests will solve your inception button problem

Related

How do I make a value not reset each page reload?

I am trying to show people their profiles from my database using PHP.
But every time a button is clicked I want the person_id ($which_person) to go up by 1 so the next person's profile is shown. I don't know how to do that because every time I click a button the page reloads so the variable "which_person" gets reset to 1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets-putinhouse/css/style1.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Festive&display=swap" rel="stylesheet">
<title>Put people in a house</title>
</head>
<body>
<?php
include "db/connection.php";
$conn = create_connection();
$which_person = 1; //This resets to "1" every time the page reloads
$getSql = "select * from Person where person_id = 1;";
$data_labels = mysqli_query($conn, $getSql)->fetch_all(MYSQLI_ASSOC);
?>
<div class="pagesize">
<h1>Put people in a house</h1>
<img src="assets-putinhouse/images/create_account.png" alt="profilepic" class="image_person">
<?php
foreach($data_labels as $labels)
{
?>
<li class="labels" data-id="<?php echo $labels['person_id'];?>">
<?php echo $labels["firstname"] ?>
<br>
<br>
<?php echo $labels["secondname"] ?>
<br>
<br>
<?php echo $labels["gender"] ?>
<br>
<br>
<?php echo $labels["descriptie"] ?>
</li>
<?php
}
?>
<br>
<form method="GET">
<input type="submit" class="slytherin_button" value="Slytherin" name="slytherin_button_name">
<br>
<input type="button" class="gryffindor_button" value="Gryffindor" name="gryffindor_button_name">
<br>
<input type="button" class="hufflepuff_button" value="Hufflepuff" name="hufflepuff_button_name">
<br>
<input type="button" class="ravenclaw_button" value="Ravenclaw" name="ravenclaw_button_name">
<br>
<input type="button" class="nextperson_button" value="Go to next person">
<p class="text_firstname">Firstname: </p>
<p class="text_name">Name: </p>
<p class="text_gender">Gender: </p>
<p class="text_info">Info: </p>
<?php
if(isset($_GET['slytherin_button_name'])) {
onFunc($conn, $which_person);
}
if(isset($_GET['gryffindor_button_name'])) {
offFunc();
}
function onFunc($conn, $wich_person){
$sqlUpdate = "UPDATE Person SET slytherin = slytherin + 1 WHERE person_id like '$which_person';"; //this does the value "slytherin" of which_person go up by 1;
mysqli_query($conn, $sqlUpdate);
$wich_person = $wich_person + 1; //which person goes up by 1 so the next person will be shown but gets reset at the start
}
function offFunc(){
echo "Button is clicked";
}
?>
</div>
<script src="assets-putinhouse/js/script.js"></script>
</body>
</html>
ยดยดยด
(Untested) Instead of:
$wich_person = 1;
It seems you could do something like:
if (isset($_GET["id"]) {
$wich_person = (int)$_GET["id"] + 1;
}
else {
$wich_person = 1;
}
and then add the following to your form:
<form action="?<?php echo $wich_person ?>" method="GET">
...
</form>
And also, unless I'm mistaken it seems you've forgot to add </form> to your code.
In any event, this code should add the current ID to your URL every time you press submit, where then it gets increased by 1.
Edit: This is intended as a quick solution based on the code structure you provided, but upon reflection not sure if it's exactly what you're looking for. More robust solutions could be had, such as cookies or sessionStorage.

Buttons that affect a function - PHP

Scenario:
I am trying to have a part of my page constently run a function, the function is then affected when the above buttons are clicked.
The buttons call functions that are within the main/constant function; master($selected).
Here is what i have tried.
index.php:
<? session_start();
require("inc.ini.php");
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>
<body>
<main>
<header>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="insert, <?$selected = "func1";?>">Func 1</button>
<button type="submit" name="change" value="insert, <?$selected = "func2";?>">Func 2</button>
</form>
<!--
I want to press a button and when it is pressed it calls the function selected and gives it the value of which the button represents.
-->
</header>
<figure>
<?
if($_POST['change']){
echo $selected;
master($selected);
}
?>
</figure>
</main>
</body>
</html>
inc.ini.php:
<? session_start();
function master($selected){
if($selected == "func1"){
function func1(){
echo "func 1";
}
}
if($selected == "func2"){
function func2(){
echo "func 2";
}
}
}
?>
Another side question. do i need to do these if statements, can the $selector jump straight to the new function.
Php is a server side language, this means that a request needs to be sent to the server to make your code run.
Also when a form submits all of it's children are sent, so it is not possible to distinguish which has been clicked.
That being said:
<?php
session_start();
require("inc.ini.php");
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta charset="utf-8">
<title>Buttons to funtions</title>
</head>
<body>
<main>
<header>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="Func1">Func 1</button>
</form>
<form method="post" action="<?=$_SERVER['php_self']?>">
<button type="submit" name="change" value="Func2">Func 2</button>
</form>
</header>
<figure>
<?php
if (isset($_POST['change'])) {
echo $_POST['change'];
master($_POST['change']);
}
?>
</figure>
</main>
</body>
</html>
If you use 2 forms then you get different values for the same name.
Looking at your inc.ini.php file it seems you're defining functions based on which input has been entered. I would suggest not doing this but if your heart is set on it then okay.
Please add a comment to this post if you need more help.
Hope this helps.

passing id on href and submitting form on same page

I am making social project form and im trying to add reply ..the reply button is a link that sends user to reply.php page with post id saved in href...on the reply.php my form action is the same page but when i click button to submit form it doesnot get the id and refreshes page with post and display error undefined id need help..
here is my reply.php
<?php
require('session.php');
$id=$_GET['id'];
//echo $id;
$submit=#$_POST['submit'];
$reply=#$_POST['reply'];
if(isset($submit)){
$id=#$_GET['id'];
$sql=mysqli_query($con,"INSERT INTO REPLY (reply,Username,post_id) VALUES '$reply','$user_check','$id'");
$sql2=mysqli_query($con,"SELECT * FROM REPLY WHERE post_id= '$id'");
while($row=mysqli_fetch_array($sql2)){
echo $row['Username']."<br/>".$row['reply']."<br/>";
}
}
else "error in submission";
?>
<!DOCTYPE html>
<html>
<title>Studhelp</title>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<?php
$sql=mysqli_query($con,"select * from posts where post_id='$id'");
while($row=mysqli_fetch_array($sql)){?>
<!-- Middle Column -->
<div class="w3-col m7">
<div class="w3-row-padding">
<div class="w3-col m12">
<div class="w3-card-2 w3-round w3-white">
<div class="w3-container w3-padding">
<h3 class="w3-opacity"><?php echo $row['Username'] ;?></h3>
<?php
echo $row['Posts']."<br/>".$row['date']."<br/>";
}
?>
</p>
</div>
</div>
</div>
</div>
<form action="reply.php" method="post">
<input type="text" name="reply" >
<input type="submit" name="submit" value="reply">
</form>
this is the anchor tag send id to reply.php
Reply
If I understand correct you need to be able to read your "id" variable after you post the form. To achieve this use $_REQUEST instead of $_GET, like this:
$id = $_REQUEST['id'];
And also pass the variable with your form, as a hidden field:
<form action="reply.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="text" name="reply" />
<input type="submit" name="submit" value="reply" />
</form>
You didn't check if the fom was submitted
Enclose other PHP code after session.php inside a conditional checker such as:
if ($_POST):
You can use form tags to create a form and put the id field as hidden.
GET requests shouldn't be use to modify data

jquery mobile form post value not working

form.php
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="lib/jmobile/jquery.mobile-1.4.5.min.css">
<script src="lib/jmobile/jquery.js"></script>
<script src="lib/jmobile/jquery.mobile-1.4.5.min.js"></script>
<head>
<body class="ui-page-theme-a">
<div data-role="page">
<div role="main" class="ui-content">
<form action = "aksi.php" method = "post">
<input type="text" name="cari" placeholder="cari informasi...">
<input type="submit" value="PROSES">
</form>
</div>
</div>
</body>
</html>
aksi.php
<?php
$lihat = $_POST['cari'];
echo $lihat;
?>
Why aksi.php cannot get value from form.php ?
if i cannot include jquery mobile that script it's working well.
JQuery Mobile will post the form via AJAX unless you add the data-ajax="false" to your form. So your PHP should be getting the Posted data under $_POST['cari'].
Tested in JSFiddle with both.
data-ajax="true" - http://jsfiddle.net/Twisty/mco3uben/
data-ajax="false" - http://jsfiddle.net/Twisty/mco3uben/1/
If you review your Console for the first, you will see a new HTTP request is posted to the action and the response echo'd back by jsfiddle. The second brings you to a new page and echos the entry.
Both should work as you have it configured. You may want to add some HTML to your PHP to allow JQM to hook into it:
<?php
$lihat = $_POST['cari'];
echo "<html><body><div data-role='page'><div role='main' class='ui-content'>\r\n";
echo $lihat;
echo "</div></div></body></html>";
?>

Php Result page to be displayed on same page

This is my coding for the search form:
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="https://excelforth.com/search1.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
The thing is , When I submit this form on my web page , It goes to the php Results page and displays the results. But the thing is that it is just a plain page with just the results displayed.
My search page :
www.mysite/test/certification-database-search/
The Php file is located in my webroot folder . I was told that for wordpress , the individual subpages cannot be edited .
How do I :
1.Display the results on the same page as the search page. Not a completely new page.
2.Retain the page layout and theme / headers/ footers of the page
3.If possible run the .php file in /certification-database-search/ and query it from there. instead of using the one in my webroot folder.
THANKS!!
Put your form and PHP in one page, PHP on top with HTML below, then use action=""
Use the variables from the inputs as $var=$_GET['var']; then echo $var;
Sidenote: If you want to stop a process, you can use die(); or exit();
You can put a message inside it; i.e.: die("Enter a search term");
A basic example:
<?php
if(isset($_GET['submit'])){
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
An alternative, showing an error message if field is empty:
<?php
if(isset($_GET['submit'])){
if(empty($_GET['query'])){
echo "Enter a search term";
}
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>

Categories