I'm new to php and twig so please don't get upset if these questions sound stupid ;)
I'm tying out twig and buttons. What I want to do:
A variable $num is 1, and is shown in the template {{ num }}, like this:
1
[change value]
The user clicks a button (named "change value") to add 1 to $num (so now, $num is 2). The template should now update and show a "2", like this:
2
[change value]
The user clicks again and its 3 and so on...
What happens in my application is:
User clicks button, the whole index.html is added to the first one, so now it shows:
1
[change value]
2
[change value]
instead of just:
2
[change value]
Now after this, if the user clicks the button again, nothing happens.
How can I "update" the variable in the template? And why can I only click the button once?
Heres my html code:
<html>
<body>
<p> {{ tempOne }} </p>
<form action="index.php" method="post">
<input type="submit" name="submit" value="change value"/>
</form>
</body>
</html>
and my php:
<?php
$twig = require_once('bootstrap.php');
$hostname = 'localhost';
$username = 'root';
$password = '';
$conn = new PDO("mysql:host=$hostname;dbname=mydb", $username, $password);
$template = $twig->loadTemplate('index.html');
$num = 1;
echo $template->render(array('tempOne' => $num));
if(isset($_POST['submit'])){
$num = $num + 1;
echo $template->render(array('tempOne' => $num));
}
Change your php code to following:
<?php
$twig = require_once('bootstrap.php');
$hostname = 'localhost';
$username = 'root';
$password = '';
$conn = new PDO("mysql:host=$hostname;dbname=mydb", $username, $password);
$template = $twig->loadTemplate('index.html');
if(isset($_POST['submit'])){
$num = $_POST['num'] + 1;
echo $template->render(array('tempOne' => $num));
} else {
$num = 1;
echo $template->render(array('tempOne' => $num));
}
And HTML to :
<form action="index.php" method="post">
<input type="hidden" name="num" value="{{tempOne}}"/>
<input type="submit" name="submit" value="change value"/>
</form>
Related
I have PHP and HTML files on my IIS site and am trying to get the output of a form shown on the same page. The issue I am running into is when I load the HTML page I am seeing the array information show as plain text on that page. I have form action = "" defined. Alternatively, when I have form action = "file.php" defined, I get the desired results, but on a new page. I took a look at the the link here but didn't seem to provide what I am looking for. I tried adding the tags on each line, which helped a bit but am still seeing the array as plain text. Here is what I have:
<form action="" method = "POST">
MAC Address of phone: <input type="text" name="phonemac"><br><br>
<input type="submit" value="Check Phone Status">
</form>
<?php
$host = "server.com";
$username = "*****";
$password = "*****";
$context =
stream_context_create(array('ssl'=>array('allow_self_signed'=>true)));
$client = new SoapClient("C:\inetpub\wwwroot\PhoneSetup\AXLAPI.wsdl",
array('trace'=>true,
'exceptions'=>true,
'location'=>"https://".$host.":8443/axl",
'login'=>$username,
'password'=>$password,
'stream_context'=>$context
));
$response = $client->getPhone(array("name"=>"$_POST[phonemac]"));
$array = json_decode(json_encode($response), true);
echo $array['return']['phone']['description'];echo '<br><br>';
echo $array['return']['phone']['name']; echo;
?>
</body>
</html>
This
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
I will write a example with your code
$array = json_decode(json_encode($response), true);
echo $array[0]['phone']
echo $array[0]['description'];
echo '</br></br>';
echo $array[0]['phone'];
echo $array[0]['name'];
your code would be like
<form action="" method = "POST">
MAC Address of phone: <input type="text" name="phonemac"><br><br>
<input type="submit" value="Check Phone Status">
</form>
<?php
$host = "server.com";
$username = "*****";
$password = "*****";
$context =
stream_context_create(array('ssl'=>array('allow_self_signed'=>true)));
$client = new SoapClient("C:\inetpub\wwwroot\PhoneSetup\AXLAPI.wsdl",
array('trace'=>true,
'exceptions'=>true,
'location'=>"https://".$host.":8443/axl",
'login'=>$username,
'password'=>$password,
'stream_context'=>$context
));
$response = $client->getPhone(array("name"=>"$_POST[phonemac]"));
$array = json_decode(json_encode($response), true);
echo $array[0]['phone']
echo $array[0]['description'];
echo '</br></br>';
echo $array[0]['phone'];
echo $array[0]['name'];
?>
<body>
</html>
How can I go to the next textbox when I press enter using pdo php? I need someone to help me to check my PHP coding. My database name is testphp_db, my table name is users.
testphp_db.php
<?php
$dsn = 'mysql:host=localhost;dbname=testphp_db';
$username = 'root';
$password = '';
try{
//Connect To MySQL Database
$con = new PDO($dsn,$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
$barcode = '';
$detail = '';
$quantity = '';
function getPosts()
{
$posts = array();
$posts[0] = $_POST['barcode'];
$posts[1] = $_POST['detail'];
$posts[2] = $_POST['quantity'];
return $posts;
}
//Search And Display Database
if(isset($_POST['search']))
{
$data=getPosts();
if(empty($data[0]))
{
echo 'Enter Barcode To Search';
} else {
$searchStmt=$con->prepare('SELECT * FROM users WHERE barcode = :barcode');
$searchStmt->execute(array(':barcode'=>$data[0]
));
if($searchStmt)
{
$user=$searchStmt->fetch();
if(empty($user))
{
echo 'No Data For This Barcode';
}
$barcode=$user[0];
$detail=$user[1];
$quantity=$user[2];
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP (MySQL PDO): Search</title>
</head>
<body>
<form action="testphp_db.php" method="POST">
Barcode: <input type="text" name="barcode" value="<?php echo $barcode;?>"><br><br>
<input type="submit" name="search" value="Search"><br><br>
Detail: <?php echo $detail;?><br><br>
Quantity: <input type="text" name="quantity" value="<?php echo $quantity;?>"><br><br>
</form>
</body>
</html>
When i insert number on my Barcode textbox and press enter, it will show the Detail of the product. My problem is the cursor for typing is disappear and it cannot go to Quantity textbox.
The cursor disappears because the form submitted. The browser POSTs to phptest.php and then loads that page. Where yhe cursor was is lost. That's how HTML forms are supposed to work.
If you just want Quantity to be focused after submitting an id search, you can consitionally set the autofocus attribute on the quantity.
Add this at the top:
$id = '';
$fname = '';
$lname = '';
$autofocus = array(
'quantity' => ''
); // this is new
function getPosts()
{
$posts = array();
$posts[0] = $_POST['id'];
$posts[1] = $_POST['fname'];
$posts[2] = $_POST['lname'];
return $posts;
}
//Search And Display Database
if(isset($_POST['search']))
{
$autofocus['quantity'] = 'autofocus'; // this is new
...
And then at the bottom:
Quantity: <input type="text" name="lname" placeholder="Last Name" value="<?php echo $lname;?>" <?php echo $autofocus['quantity']; ?>><br><br>
Or, instead of the above changes, you could use Javascript to make an AJAX request to a page that would perform the search without submitting the form. Since you have no JS I'm not going to provide an example (unless you specifically ask for one).
I have a form like this: The file name is "main.php"
<html>
<form action = "options.php" method = "POST" />
<p> <h3> Enter Phone Number: </h3> <input type = "text" name =
"cust_phone" />
<p> <input type = "submit" value = "Submit" />
</form>
</html>
The "options.php" looks like this:
<html>
<body> Details of: <?php session_start();
echo htmlentities($_POST["cust_phone"]) . "<br>";
$link = oci_connect('hd','hd', 'localhost/mydb');
if(!$link) {
$e = oci_error();
exit('Connection error ' . $e['message']);
}
$_SESSION['var'] = $_POST["cust_phone"];
echo $_SESSION['var'];
$ph = htmlentities($_POST["cust_phone"]);
$q1 = "select CUST_ID from customer where CUST_PHONE = :bv_ph";
$q1parse = oci_parse($link, $q1);
oci_bind_by_name($q1parse, ':bv_ph', $ph);
oci_execute($q1parse);
oci_fetch($q1parse);
$res = oci_result($q1parse, 'CUST_ID');
if(!$res) {
echo "No Order found. New Order?";
}
?>
<form action = "" method = "POST" >
<input type = "radio" name = "option" value = "Yes" checked> Yes
<br>
<input type = "radio" name = "option" value ="No"> No <br>
<input type = "submit" value = "submit">
</form>
<?php
if(isset($_POST['option']) && ($_POST['option']) == "Yes") {
header("Location: newcustomer.php");
}
elseif(isset($_POST['option']) && ($_POST['option']) == "No") {
header("location: main.php");
}
?>
</body>
The "newcustomer.php" looks like this:
<!DOCTYPE HTML>
<html>
<body>
<form action = "order.php" method = "POST" >
<p> Phone Number: </p>
<p> Enter Address: <input type = "text" name = "address" /></p>
<p> Enter Area: <input type = "text" name = "area" /></p>
</form>
<?php
session_start();
echo $_SESSION ['var'];
?>
</body>
</html>
I want the value of the number entered by the user in "main.php" to be used in "newcustomer.php" which I am not able to achieve. Am i using the session variable correctly? The result when I run "newcustomer.php" does not show any error but does not echo the "$_SESSION['var']".
You should write session_start() every page topper ...
<?php
session_start();
?>
<html>
.....
......
Hello Every One i am new to coding and now i am learning php and html below i submited the code and i want to print Hello World in a new page if the given user name and password is correct but it is printing in the same page can any one help me Thanks in Advance
<!doctype html>
<html>
<body>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel">
</form>
<?php
$a = 123;
$b = 234;
$c = $_POST["username"];
$d = $_POST["password"];
$e = "Hello World!!";
$f = "Error 404 Page Not Found";
if(isset($_POST['submit'])){
if ($a == $c && $b == $d )
{
print "$e";
}
else
{
echo "$f";
}
}
?>
</body>
</html>
you could use the header function and it would look like this:
if ($a == $c && $b == $d )
{
header("location: newpage.php");
exit;
}
else
{
echo "$f";
}
or indeed as stated above me redirect the form to the new page
First of all this will be the html part in a separate part like login.html:-
<!doctype html>
<html>
<body>
<form action="login.php" method="post"><!-- if you will not give action then form will posted to the same page -->
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel"><!-- about this i cannot say anything -->
</form>
</body>
</html>
Now in login.php:-
<?php
$original_user_name = 123; // take variable name that are self descriptive
$original_user_password = 234;
$form_username = $_POST["username"];
$form_password = $_POST["password"];
// $e = "Hello World!!"; no need of extra variable
// $f = "Error 404 Page Not Found"; no need of extra variable
if(isset($_POST['username']) && isset($_POST['password'])){ // check with POSTED values not with button value
if ($original_user_name == $form_username && $original_user_password == $form_password){
echo "hello World!";
}else{
echo "Error 404 Page Not Found!";
}
}else{
echo "please fill both user name and password!";
}
?>
Note:- both files must be in the same working directory.
Create a new .php file where you can put your PHP code & in the html file, change form tag to < form action="path of that php file" method="post">
I never really used session so it could be some stupid mistake. When I use if(isset($_SESSION) it outputs false, I think it has something to do with the foreach. I get no errors whatsoever. Could anyody spare some time to help me?
<?php
session_start();
if(isset($_POST['register']))
{
require_once('../resources/library/register.class.php');
//require_once('../resources/library/sessions.class.php');
$obj_reg = new register();
$name = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];
$checking = $obj_reg->checking($name, $pass);
//An foreach for converting POST data inside SESSION variables
//isset checks if the array value contain post variables
$posts = array($name, $pass, $email);
foreach ($posts as $p)
{
if(isset($_POST['p'])){
$_SESSION['p'] = $_POST['p'];
}
}
}
?>
<form method="post" action="index.php?page=register.php">
<table>
<tr><td>username:</td><td> <input type="text" name="user"></td></tr>
<tr><td>password:</td><td> <input type="password" name="pass"/></td></tr>
<tr><td>email:</td><td> <input type="text" name="email"/></td></tr>
<?=( !empty( $checking ) ) ? $checking : '' ?>
</table>
<input type="hidden" name="token" value="<?=$token;?>"/>
<input type="submit" name="register" value="register"/>
</form>
<?php
session_start();
if(isset($_SESSION['p']))
{
echo "mama";
}
else
{
echo "why?";
}
?>
You need to call session_start on every page that needs $_SESSION.
I think you also mean to use $_SESSION[$p] = $_POST[$p] instead of the string 'p'.