How to get php function ouptut on client side?

I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as&nbsp;<strong>name</strong>&nbsp;&amp; in return I want output as&nbsp;<code>Hello name</code>. I tried, but it's not showing,

I am trying to execute PHP function(server-side) on button click in html (client-side). I want to pass a parameter to PHP function as name & in return I want output as Hello name. I tried, but it's not showing,

Server-side

The PHP file name is "name.php" having function greet() with parameter $name is as follows:

<?php
function greet($name)
{
   echo "hello $name";
}
?>

Client-side

The HTML file consists of a button "Click me" which should send the name John to PHP page, and the greet() function should execute and output should display at client side as "Hello John" is as follows:

<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("#button").click(function(){
$.ajax({
type: "POST",
url: "name.php",
data: { name: "John" }
}).done(greet(data)
{
alert( "Data Saved: " + data);
});
});
});
</script>

<input type="button" id="button" value="Click me">

</html>

I have used Ajax method for calling PHP function if any other POST method can give output, then please let me know.

Can someone please help to how to get output from PHP function to client-side on button click.

How to Include PHP in HTML

How to Include PHP in HTML

Welcome to this article on how to use PHP in HTML! Here, we try to get specific about exactly how PHP and HTML interact, at the level of a specific .php file. In other words, how do you actually include PHP in HTML, and what rules can and can’t you follow in weaving PHP and HTML together?

Welcome to this article on how to use PHP in HTML! Here, we try to get specific about exactly how PHP and HTML interact, at the level of a specific .php file. In other words, how do you actually include PHP in HTML, and what rules can and can’t you follow in weaving PHP and HTML together?

As a last note: to learn what works and doesn’t for using PHP in HTML, concrete examples are more helpful than theory. So the bulk of this article is examples of PHP’s proper use within HTML, with comments for each code example.

How to Include PHP in HTML: File Types and Other Considerations

The first thing to know is that, by default, you can’t use PHP in HTML files, meaning files that end with .html. It’s possible to configure your server to allow PHP in .html files, but that’s outside our scope—so for now, just remember that, if you want to write PHP, you want to be working with .php files.

In a WordPress environment, that’s largely taken care of: the entirety of WordPress is PHP-based, so you will very frequently be working with .php files, and almost never with .html files.

The other thing you should know is this: a .php file can run exactly like a .html file, with absolutely no PHP. In other words, there’s nothing about a .php file that makes it inherently “different” than a .html file, except that it can run PHP.

A few code examples will make it clearer what we mean by this.

HOW .HTML FILES RESPOND TO HTML AND PHP

Imagine we have a file, index.html, that has the following contents:

<!-- Filename: index.html -->
<h1>I am HTML markup.</h1>

Accessing this index.html file in your web browser would give you the following output: <h1>I am HTML markup.</h1>

Now what if we added the following to index.html:

<!-- Filename: index.html -->
<h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?>

Accessing this index.html file in your web browser would give you the following output: <h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?> Definitely not what we want. The issue is that, by default, HTML files don’t “speak” PHP.

HOW .PHP FILES RESPOND TO HTML AND PHP

Now, what if we simply renamed index.html to index.php and ran both examples again?

<!-- Filename: index.php -->
<h1>I am HTML markup.</h1>

This would output <h1>I am HTML markup.</h1>, exactly as before. In other words, there’s no need to actually write PHP into .php files: PHP files handle plain HTML just fine.

Here’s the second example:

<!-- Filename: index.php -->
<h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?>

This would output the following clean HTML: <h1>I am HTML markup.</h1><p>And I am PHP.</p>

This example demonstrates that PHP files (that is, .php files) can automatically interpret PHP code—anything inside the <?php ?> tag—and turn the resulting output into HTML.

Basic Use of PHP in HTML

Here are the basics of how to include PHP in HTML. This relies on knowledge of PHP’s echo statement, which we’ve covered in a previous article.

PRINTING HTML CONTENT WITH PHP’S ECHO

To output HTML content within PHP, echo it:

<div><?php echo 'Hello'; ?></div>

Output to browser will be: <div>Hello</div>

OUTPUTTING HTML TAGS USING PHP

You can use PHP to output HTML tags into the page’s markup:

<div><?php echo '<p>Hello</p>'; ?></div>

Output to browser will be: <div><p>Hello</p></div>

USING PHP INSIDE HTML TAGS

PHP can go anywhere, including inside HTML tag declarations, and including inside quotes ("):

<div class="<?php echo 'big-element'; ?>">Hello</div>

Output to browser will be: <div class="big-element">Hello</div>

Details on Opening and Closing the PHP Tag in HTML

This section relies on a basic understanding of PHP functions, which we’ve covered in an earlier article.

OPENING AND CLOSING THE PHP TAG

You can swap between HTML and PHP at any time by opening (<?php) and closing (?>) the PHP tag:

<?php echo '<p>I came from PHP.</p>' ?><p>And I came from HTML.</p><?php echo '<p>Back to PHP and '; ?>now HTML.</p>

Output to browser will be: <p>I came from PHP.</p><p>And I came from HTML.</p><p>Back to PHP and now HTML.</p>

LINE BREAKS IN PHP CODE

Line breaks (as well as spaces and indentation) can work any way within the PHP tag:

<?php

echo '<p>I am PHP.</p>';

echo '<p>Still PHP.</p>';

?><p>Now HTML.</p>

Output to browser will be: <p>I am PHP.</p><p>Still PHP.</p><p>Now HTML.</p>

HTML INSIDE PHP OPERATORS

HTML can go inside PHP operators of all kinds–such as if()-statements and functions, and will simply print like echo when the relevant lines of code are run.

<?php function print_greeting() { ?>
<p>HTML output from inside function.</p>
<?php } ?>

<?php print_greeting(); ?>

Output to browser will be <p>HTML output from inside function.</p>

Another way to describe this code example is to note that you can close a PHP tag, and revert to plain HTML, insidea function definition, if()-statement, or other PHP operator.

Those lines of HTML will print to the page when they are executed, which depends on the control flow of the PHP logic on the page.

DECLARATIONS FROM PREVIOUS <?PHP ?> TAGS ARE STILL STORED

PHP will remember variables, functions, and other declarations from previously opened and closed PHP tags higher on the page:

<?php function print_greeting() { ?>
<p>Hello from function.</p>
<?php } ?>

<div>Plain HTML between two PHP tags.</div>

<?php $my_string = '<p>Hello from variable.</p>'; ?>

<div>Plain HTML between two PHP tags again.</div>

<?php print_greeting();
echo $my_string; ?>

Output to browser will be: <div>Plain HTML between two PHP tags.</div><div>Plain HTML between two PHP tags again.</div><p>Hello from function.</p><p>Hello from variable.</p>

Controlling HTML Output with PHP Operators

PHP can control the logic flow across the page, altering output.

ITERATING HTML OUTPUT USING A WHILE() LOOP

Here’s an example that iterates (repeats) HTML output using a PHP while() loop:

<p>
$i = 0; // $i starts out at 0
// This while()-loop will keep running as long as $i is less than 3
<?php while( $i < 3; ) : ?>
<span>Hello</span>
$i++; // This means "increase $i by 1"
<?php endwhile; ?>
</p>

Output to browser will be: <p><span>Hello</span><span>Hello</span><span>Hello</span></p>

INSERTING DYNAMIC VALUES INTO HTML USING PHP

Inserting dynamic values—values that are not pre-defined, but change as variables—into HTML using a PHP while() loop:

<p>
<?php while( $i = 0; $i < 3; $i++ ) : ?>
<span>Number <?php echo $i; ?></span>
<?php endwhile; ?>
</p>

Output to browser will be: <p><span>Number 0</span><span>Number 1</span><span>Number 2</span></p>

This basic pattern—dynamic HTML output within a PHP while loop—is shared by WordPress’s content engine, the Loop.


CONTROLLING HTML OUTPUT USING PHP CONDITIONALS

You can control HTML output using PHP conditionals (if()-statements):

<?php if( 1 === 1 ) { ?>
<p>Now you see me</p>
<?php } ?>

<?php if( 1 === 2 ) { ?>
<p>Now you don't</p>
<?php } ?>

Output to browser will be: <p>Now you see me</p>

In the example above, we asked two things, one which is always true and one which is always false:

  1. “Does 1 equal 1?” (This is always true, so the code inside its if statement will alwaysrun, printing that HTML to the page.)
  2. “Does 1 equal 2?” (This is never true, so the code inside its if statement will neverrun, and its HTML does not print to the page.)
And That’s Our PHP-in-HTML Primer

Hopefully these code examples have given you a good sense of some of the basics of how to add PHP to HTML, and how PHP and HTML interact in practice.

Thanks for reading! If you liked this post, share it with all of your programming buddies!

Originally published on wpshout.com

HTML vs HTML5 - What's the Difference?

HTML vs HTML5 - What's the Difference?

This comparison on HTML vs HTML5 is intended to provide a crisp knowledge about both the markup languages and guide you to find the suitable one if you are building your own web page or web application.

This comparison on HTML vs HTML5 is intended to provide a crisp knowledge about both the markup languages and guide you to find the suitable one if you are building your own web page or web application.

History of HTML

The history of the hypertext markup language is a strange and interesting tale. The man behind HTML, Tim Berners-Lee was putting together his first elementary browsing and authoring system for the Web and created a quick little hypertext language that would serve his purposes.

But the problem lied in the simplicity of the language. HTML was text-based and anyone could use any editor or word processor to create or convert documents for the Web. The developers started implementing new features in their browsers and started releasing advanced versions of HTML.

Introduction to HTML & HTML5

HTML stands for Hyper Text Markup Language and it is the standard markup language for creating web pages and web applications. It is used to describe the structure of Web pages using markup.

HTML5 is the 5th version of HTML and is a core technology markup language of internet that is used for structuring and presenting the content for www.

Comparison between the Two

Now lets have a look at all the new elements that were added in HTML5 and the factors that make it better than HTML.

Let’s look at the factors we will be using for the comparison on HTML vs HTML5:
Video & AudioVector GraphicsStorageWeb Browser SupportEase of Use

Video & Audio

The first factor in HTML vs HTML5 is the introduction of audio and video in HTML5. There was no such concept of media in case of HTML. But it is one of the integral part of the 5th Version.

Vector Graphics

Vector graphics was used in HTML with the help of various technologies such as VML, Flash, Silverlight etc. But it is an integral part of HTML 5 such as canvas and svg. This was a new addition to the revised version.

Storage

The third factor of comparison is storage. In case of HTML, we can use the browser cache as the temporary storage whereas in case of HTML5, **application cache, web SQL database **and web storage is used.

Web Browser Support

Now for the first version of HTML, all the old web browsers run smoothly for creating web pages. For HTML5, the new browsers have started supporting its specifications. Some of the browsers include mozilla firefox, chrome, opera etc.

Ease of Use

While HTML5 does have risks like** constant updates**, it is generally easy to keep up with the changes and updates because of the simpler syntax as compared to other versions of HTML. For instance, you have a very simple declaration at the start of the page to set it as an HTML5 page, ie, the doctype declaration.

New Elements in HTML5

The comparison on HTML vs HTML5 also includes new elements and tags that have been added to the latest version. HTML5 comes with a number of new elements and eliminates several others. Some of the tags were discarded in the new HTML version and the usage of tags were changed accordingly.

Let’s have a look at some of the important tags that were added in HTML5:

Audio & Video

The **audio tag **and the video tag are the two major additions to the HTML5. The audio tag and the video tag enables developers to embed music and audio on their website.The audio & video tags also have a number of attributes for additional controls.

<audio autoplay=”autoplay” controls=”controls”>
<source src=”music.ogg” />
<source src=”music.mp3″ />
</audio>

There are a few other multimedia attributes and elements that come with HTML5, like the track element that provides text tracks for the video element.

Canvas

One of the key added elements in HTML5 is which has hugely impacted the use of Adobe Flash in websites. The element can be used to draw graphics with various shapes and colors via scripting, usually JavaScript. The element is simply a container for the graphics.

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #000000;”>
</canvas>

Header > & < Footer

Another big addition that HTML5 comes with are the new and elements, indicating a new web anatomy. With these new tags, there is no longer a need to identify the two elements with a

tag.

<header>
<h1>Title </h1>
<div class=”post-meta”>
<p>By Author</a> <span class=”category”>Filed in Web 2.0</span></p>
</div>
</header>
<article>
<p>Body of text</p>
</article>

The footer is placed at the bottom of the web page but it can also be placed at the end of a page section.

<header>
<h1>Title </h1>
<div class=”post-meta”>
<p>By Author</a> <span class=”category”>Filed in Web 2.0</span></p>
</div>
</header>
<article>
<p>Body of text</p>
</article>

Menu

Given the fact that the web has changed into more just linked paged and documents, the tag is a welcome addition for greater web interactivity. It represents menu commands for simplicity in desktop and mobile applications.

<body contextmenu=”new-context-menu”>

<menu id=”new-context-menu” type=”context”>

<menuitem>Hello World</menuitem>

</menu>

</body>

Now with this, we come to an end of this comparison on HTML vs HTML5. I Hope you guys enjoyed this article and understood the difference between the two versions and how HTML5 is better and simpler in use. So, if you have read this, you might have a clear idea about which version of HTML you should opt for, while building your own web page.

Cannot send array to php via AJAX?

Well I followed&nbsp;<a href="https://stackoverflow.com/questions/9001526/send-array-with-ajax-to-php-script" target="_blank">this</a>&nbsp;question, but cannot send array to php it returns me always empty:

Well I followed this question, but cannot send array to php it returns me always empty:

JS:

            values = [];
            values['mpsRegnomer'] = $('#mpsRegnomer').val();
            values['mpsMarka'] = $('#mpsMarka').val();
            values['mpsMarkaOther'] = $('#mpsMarkaOther').val();
            values['engineType'] = $('#engineType').val();
            values['seatNumberInput'] = $('#seatNumberInput').val();
            values['carColor'] = $('#carColor').val();
            values['mpsChassiNum'] = $('#mpsChassiNum').val();
            values['mpsModel'] = $('#mpsModel').val();
            values['mpsModelOther'] = $('#mpsModelOther').val();
            values['mpsManufactureDate'] = $('#mpsManufactureDate').val();
            values['mpsfor'] = $('#mpsfor').val();
            values['VehicleType'] = $('#VehicleType').val();
            values['dvigatelInput'] = $('#dvigatelInput').val();
            values['engineMaxPower'] = $('#engineMaxPower').val();
            values['is_automatic'] = $('#is_automatic').val();
            console.log(values);
        $.ajax({
                type: 'POST',
                url: 'assets/clients/services/saveDataMPS.php',
                async: false,
                dataType: "JSON",
                data: {"values": JSON.stringify(values)},
                success:function(response){
                    alert(1);
                }
        });

console.log(values) show me that array is OK.

PHP code:

<?php

var_dump(json_decode($_POST["values"])); exit;

It returns me always empty, also tried only with var_dump($_POST); same result.. Where am I wrong?

Result from console.log(values):