add text to random textarea in html (php) - php

I would like to know if something like this is possible for php:
Very-Pseudo-Code:
<body>
<textarea name="text1">
</body>
<?php
$A=mysqli_connect(...);
if($A){**text1.echo "Connection established";**}
$B=mysqli_select_database(...);
if($B){**text1.echo "Database selected";**}
?>
I want a life feedback that tells the user what is going on in the background. Kind of a life feed that consistently gives the user information. I played around with GET and POST methods and also with PHP_SELF, but nothing worked out.
In javascript it seems to be quite simple with something like
document.myform.append()
Is there something like this for php, or do i have to mix it with javascript?
(This is my first html/php project, i hope its not a silly question)

Related

New element on a php array file

I'm a total noob on php, but a friend of mine asked for help and I thought I might do it..
I have this code/file here and want to find a way to add an element in this array via html file. I know it sounds noob, it does for me too, but please help, I've seen arrays, vectors and lists only on c++, tried to take a look at the documentation of php5 (since he want it in php 5) but I couldn't make it!
here it is...
<?php
$bledi = array('user12345', 'user2016', 'user5749852', 'user985658', 'HowToAddANewElement');
echo $result;
?>
You need a form with a text input in HTML and a send button.
When the form is submitted to the action page there you could put
your php.
You can make a global array ($bledi) and you will have an if condition there that says something like
if($_REQUEST['input_text'])
$bledi[] = $_REQUEST['input_text'];

Raw PHP code converted for use in CakePHP, new to cake

I'm a total newbie to CakePHP, but I have a fairly ok grasp on the fundamentals of raw PHP. My problem is getting over the hump and learning how to conform to the CakePHP framework. I've read up on Cake and have done the examples, but the only way I'm going to learn this is to jump in head first and start coding something on my own. My question is, how do I convert something this simple into cake. IE what goes where, Controller/Model/View.
<?php
if (isset($_POST['guess'])) {
//my php
};
?>
<form name="form" method="post" action="#">
Your guess: <input type="text" name="guess">
<input type="submit" value="Submit">
</form>
All I want to do here is pass a number from user input to php (no database connection), run some php calculations, then return the results to the view. Do I have to use the form helper? It seems to center around database queries... And what goes where? From my angle, it seems cake may complicate such a simple snippet.
Much appreciated if someone could make some connections/relations to what I'm use to, and get me rolling.
I recommend to read CakePHP´s Blog Tutorial.
At first, create a view located in app/View/Guess/create.ctp:
// Using Form-Helper
<?php echo $this->Form->create(); ?>
<?php echo $this->Form->input('guess'); ?>
<?php echo $this->Form->end('Submit'); ?>
At second, create a controller located in app/Controller/GuessController.php:
class GuessController extends AppController
{
// stop using a model
public $uses = array();
function create()
{
if($this->request->is('post', 'put'))
{
// now do something with your form data
$doSomething = $this->request->data['Guess']['guess'];
}
}
}
That´s it.
Now you should call your action via url http://examplehost.com/Guess/create
At the beginning, CakePHP seems to be complicated, but it isn´t. I think, it is important to understand the MVC. That makes it easier to learn all common PHP-Frameworks.
At a first glance, a simple form like above might be implemented easier via pure php and html code. But if you want to do deeper tasks (e.g. validation, authentication, autorisation, security ...) and want to have a clean code, you will recognize the advantages of that framework.

Using php to create a button that calls a php function [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
Currently I have the following but it doesn't like me very much:
1. index.php:
<!DOCTYPE html>
<html>
<body>
<div id = "bookList">
<?php
include("list.php");
?>
</div>
</body>
</html>
2. list.php:
<?php
echo '<button id = "read">Read</button><br><br>';
echo
"<script type=\"text/javascript\">
$(\"#read\").click(function()
{
alert(\"<?php display(); ?>\");
});
</script>";
function display()
{
echo "hello";
}
?>
As is hopefully obvious from the code I posted above, I am attempting to create a button using php which when clicked on will in turn call a php function. I have not been successful as of yet. Any advice will be appreciated but I'd like my code to stay as close to what I currently have as possible.
Some basic misunderstandings here. PHP and Javascript does not interact like that
Let me give a rather silly analogy:
You go to the coffee shop and order some coffee.
The shop keeper gives you the coffee. You take a sip and find you need more sugar
You tell the shopkeeper who adds the sugar to your coffee. Lets call this action addSugar().
Everything is cool here. Now what happens in this second scenario:
You go to the coffee shop and order some coffee.
The shop keeper gives you the coffee. You take the coffee with you and walk home
You take a sip and find you need more sugar
You tell the shopkeeper to addSugar()...? erm...
Well the shopkeeper aint there. In the same way, when a page reaches your browser, it has left the coffee shop. There is no PHP/shopkeeper around anymore
Your request to addSugar() that you are trying on button click using Javascript will not work.
What you will need to do is use something called AJAX which is a way to quickly run to the coffeshop just for adding a little sugar.. Its a broader topic and you will need to read about it, but there are tons of resources out there..

Is there a way to access previously created divs?

I'm creating divs with dynamic names using php such as:
echo "<div class=\"".$row['country']."\">"
So it's going to first create a series of divs such as ...
<div class="America">
//stuff goes in here
</div>
<div class="Germany">
//stuff goes in here
</div>
<div class="Singapore">
//stuff goes in here
</div>
But later on in the code after the "Germany" div has already been created, I'm going to make another mysql query to a different table and I want to access the "Germany" class and add content in it. It then becomes something like...
<div class="Germany">
<p> Germany has x number of people </p>
<p> The most popular car in Germany is x </p>
</div>
I know with Jquery there is the append() function. Is there something similar in PHP where I can access a div that's already been created and add stuff to it?
Note: all of this is in one php file that loads all the content when the page loads.
It can be done. You can use output buffering to capture all output. Then you can use an HTML DOM parser to modify that output. After that, you can flush the lot.
But this will work only once and it will seriously slow down your script. Don't do it, it smells of bad design.
Your best option would be to create multiple variables like
$css['America'];
$css['Germany'];
$general_output;
etc and, while you are building your site, just add info to the required variable.
Once you get to the end of the page, print them in the right order and you are done.
I think you can create an array like
$div["Amereica"] = "America"
And in future you can append value to this array. eg.
$div["Amereica"] .= 'text to append';
And finally you can use implode function or using other array functions you can crate final html.
If you really need to re-access that element, you're probably looking for DOM functions or phpQuery
Simple answer is no.
You can't change HTML code produced by PHP, because that code is already sent to browser, and PHP works on server side.
One of complicated answers is #GolezTrol answer.

PHP error displaying position.

I have following PHP code:
if($_SESSION['msg']['login-err'])
{
echo '<p class="error">'.$_SESSION['msg']['login-err'].'</p>';
unset($_SESSION['msg']['login-err']);
}
Everything working fine. 'P' with error is displayed correctly inside element.
My question is how to specify where my error should be displayed?
E.G. Not as the first element inside the form, but
as the second for example
just before <input name="user" />
??
Any suggestion much appreciated.
Since you're (somewhat naively) testing for errors and displaying errors at the same time, you should just move the code lower in the page so that the test/output happen where you want it to display.
Ideally you should decouple your display code from your logic. Store your errors for later display in an $errors variable which is passed to your template to be rendered.
Rather than placing the echo where it exist now, why not change your code to something like this:
$error;
if($_SESSION['msg']['login-err'])
{
$error = $_SESSION['msg']['login-err'];
unset($_SESSION['msg']['login-err']);
}
Then, right above your input tag, do something like this...
<?php echo $error;?>
It's not the best way to do error handling...but it should do what you are asking.
The "where" depends on you frontend design.
In a login form I would recommend to display errors at the top or bottom of the whole form. In a contact/registration form it makes more sense to show error beside the field ( on the top or left ).
Quite different thing is this combination of logic and presentations. They should be separated, because each piece of code should have only one reason to change. Your piece has two reasons: design or logic.
You would gain a lot if you did some extended studies in MVC and OOP in general.

Categories