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>
Related
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'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>
I have been trying to make Twitter reply code using PHP and it didn't work. When I run it it just post it as a tweet without the reply to the tweet id.
in_reply_to_status_id doesn't work when I run the code.
is there any solution?
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="submit" name="do"value="do"></input>
<input type="text" name="tweetid" placeholder="tweet id"></input>
<input type="text" name="message" placeholder="message"></input>
</form>
<?php
require_once('twitteroauth.php');
$connect=mysql_connect ("localhost","___","___");
mysql_select_db ("___");
if(isset($connect)){
$do=$_POST ['do'];
$message=$_POST ['message'];
$tweetid=$_POST ['tweetid'];
$consumerKey = '___';
$consumerSecret = '___';
$accessToken= '___';
$accessTokenSecret= '___';
if(isset($do)){
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken,$accessTokenSecret);
$tt=$tweet->post('statuses/update', array ('status'=>$message , 'in_reply_to_status_id' =>$tweetid));
}}
else {
print "error";
}
mysql_close ();
?>
I have a html form which collects, name, place, email on submission it posts to csv file. But the php file creates a file without any data. Can any body help me in correcting the code. The full code is given below.
HTML form CODE:
<form method="post" name="excisedata" action="csv.php">
Enter Full Name: <br>
<input type="text" name="name"><br>
Enter Place /Location : <br>
<input type="text" name="place"><br>
Enter Valid Email :<br>
<input type="text" name="email"><br>
<input type="submit" name='submit' onclick="show_confirm()" value="SUBMIT">
</form>
PHP CODE
<?php
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a");
foreach ($list as $line) {
fputcsv($file,explode(',',$line)); }
fclose($file);
?>
first, i would add static values to your fputcsv function to make sure you have no file permissions problems.
fputcsv($file,'bird,dog,fish');
once you confirm that is ok, try this:
your form is posting data, your processor is looking for GET data.
change
$name = $_GET["name"];
$place = $_GET["place"];
$email = $_GET["email"];
to
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
<?php
$name = $_POST["name"];
$place = $_POST["place"];
$email = $_POST["email"];
$list = array($name, $place, $email,);
$file = fopen("mylist.csv","a+");
fputcsv($file,$list);
fclose($file);
?>
I want to add new data to an array dynamically using HTML and PHP
Here is my code:
<form action="" method="">
<input type="text" name="name">
<input type="submit" name="send">
</form>
<?php
if(isset($_POST['send']))
{
$name = $_POST['name'];
for($=1;$i<2;$++)
{
$name = array($name);
$names = array_push($names,$name);
}
print_r($names);
}
?>`
Does anyone have a better way?
I prefer to create indexes after its declaration:
$test = array();
$test['a'] = 'value';
$test['b'] = 'value';
$test['c'] = 'value';