{"id":144,"date":"2018-07-01T21:07:00","date_gmt":"2018-07-01T18:07:00","guid":{"rendered":"https:\/\/html-online.com\/articles\/?p=144"},"modified":"2020-01-14T10:55:54","modified_gmt":"2020-01-14T08:55:54","slug":"javascript-variable-php-mysql-ajax-post-json","status":"publish","type":"post","link":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/","title":{"rendered":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post"},"content":{"rendered":"<p><strong>We will show you the way how to save <a href=\"\/articles\/category\/javascript\/\">JavaScript<\/a> variables to a PHP\/MySQL DataBase easily and securely. In my demonstration we&#8217;ll go through an example in which we want to store an array for my users. The users can be identified by their unique names.<\/strong><\/p>\n<p>Log in to to your <a href=\"https:\/\/www.cpanel.net\/\" target=\"_blank\" rel=\"external nofollow noopener noreferrer\">cPanel<\/a> hosting, find the &#8220;<strong>MySQL Databases<\/strong>&#8221; in the menu and create a new database. Create a user and add it to the database, checking all privileges. We&#8217;ll use this user to connect to the DB.<\/p>\n<p>Open <strong>phpMyAdmin<\/strong> and you should see the new DB in the list. Select it and make a new table. In my example I&#8217;ll call it <em>usertimes<\/em>.<br \/>\nSet to auto increment (A_I) the primary key and make sure the name field is a unique value. We&#8217;ll store the current save date and the rest of the variables depends on your specifications. Save the settings when it&#8217;s done. My database is ready to receive input.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"\/articles\/wp-content\/uploads\/2017\/02\/database-structure.png\" alt=\"JavaScript variables to PHP MySQL DataBase\"><!--more--><\/p>\n<h2>Writing in the DataBase<\/h2>\n<p>The JavaScript function below collects the variables and posts them to the <strong>savesettings.php<\/strong> file.<br \/>\nThe #saveWarningText div will display the success message returned from the PHP file or the error message if something went wrong.<br \/>\nThe three variables we&#8217;re passing are the <em>name<\/em>, <em>amount<\/em> and <em>times<\/em> because the id is automatically incremented and the date can be generated on the server side.<\/p>\n<pre style=\"background: #fff; color: #000;\">function <span style=\"color: #0000a2; font-weight: bold;\">saveUserTimes<\/span>() {\n    <span style=\"color: #687687;\">$<\/span>.post(<span style=\"color: #d80800;\">\"savesettings.php\"<\/span>,\n    {\n        name: <span style=\"color: #687687;\">$<\/span>(<span style=\"color: #d80800;\">\"#userName\"<\/span>).val(),\n        amount: aGlobalVariable,\n        times: <span style=\"color: #d80800;\">'1,2,3,4,5,6,7'<\/span>,\n    },\n    function(data,status){\n        <span style=\"color: #6d79de; font-weight: bold;\">document<\/span>.<span style=\"color: #3c4c72; font-weight: bold;\">getElementById<\/span>(<span style=\"color: #d80800;\">\"saveWarningText\"<\/span>).innerHTML <span style=\"color: #687687;\">=<\/span> data;\n        <span style=\"color: #687687;\">$<\/span>( <span style=\"color: #d80800;\">\"#saveWarningText\"<\/span> ).fadeIn(<span style=\"color: #cd0000; font-style: italic;\">100<\/span>);\n        <span style=\"color: #3c4c72; font-weight: bold;\">setTimeout<\/span>(function(){ <span style=\"color: #687687;\">$<\/span>( <span style=\"color: #d80800;\">\"#saveWarningText\"<\/span> ).fadeOut(<span style=\"color: #cd0000; font-style: italic;\">100<\/span>); }, <span style=\"color: #cd0000; font-style: italic;\">3000<\/span>);\n    });\n}\n<\/pre>\n<p>Create a PHP file to decode the post parameters and insert them into the DB.<br \/>\nAt the beginning of the file we specify the database connection string.<br \/>\nNext we receive the three variables passed with the post method and escape the string to avoid SQL injections and ensure security.<br \/>\nRemember that JavaScript is executed on the client-side so everyone check and see the parameters we&#8217;re posting to our PHP file. Knowing our post variables hackers can try to execute database queries through our file. This is why beside the string escaping I&#8217;m also testing the length of the &#8216;times&#8217; string and I&#8217;m not allowing to store it if the data is too large. You can include additional security steps to avoid hackers. Allowing only a limited amount of queries from one IP address can reduce the risk that someone will flood our database.<br \/>\nIn the following lines we create the $sql query. We insert into the <em>usertimes<\/em> database table the name, date, amount and times variables. The id is automatically set, and the date is using the <em>CURDATE()<\/em> query. In case the current name value already exists in the database the 3 other variables are updated in that row. This is how the duplicate key is handled in my example.<\/p>\n<p><strong>Contents of savesettings.php file:<\/strong><\/p>\n<pre style=\"background: #fff; color: #000;\">&lt;?php\n<span style=\"color: #0206ff; font-style: italic;\">$servername<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"localhost\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$username<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"databaseUserName\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$password<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"userPassword\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$dbname<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"databaseName\"<\/span>;\n\n<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0100b6; font-weight: bold;\">new<\/span> <span style=\"color: #6d79de; font-weight: bold;\">mysqli<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$servername<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$username<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$password<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$dbname<\/span>); <span style=\"color: #00b418;\">\/\/ Create connection<\/span>\n<span style=\"color: #0100b6; font-weight: bold;\">if<\/span> (<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>connect_error) {     <span style=\"color: #00b418;\">\/\/ Check connection<\/span>\n<span style=\"color: #0100b6; font-weight: bold;\">    die<\/span>(<span style=\"color: #d80800;\">\"Connection failed: \"<\/span> <span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>connect_error);\n} \n\n<span style=\"color: #0206ff; font-style: italic;\">$name<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">mysqli_real_escape_string<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$_POST<\/span>[<span style=\"color: #d80800;\">'name'<\/span>]);\n<span style=\"color: #0206ff; font-style: italic;\">$amount<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">mysqli_real_escape_string<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$_POST<\/span>[<span style=\"color: #d80800;\">'amount'<\/span>]);\n<span style=\"color: #0206ff; font-style: italic;\">$times<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">mysqli_real_escape_string<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$_POST<\/span>[<span style=\"color: #d80800;\">'times'<\/span>]);\n\n<span style=\"color: #0100b6; font-weight: bold;\">if<\/span> (<span style=\"color: #3c4c72; font-weight: bold;\">strlen<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$times<\/span>) <span style=\"color: #0100b6; font-weight: bold;\">&gt;<\/span> <span style=\"color: #cd0000; font-style: italic;\">200000<\/span>) {  <span style=\"color: #0206ff; font-style: italic;\">$times<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"\"<\/span>;    }\n\n<span style=\"color: #0206ff; font-style: italic;\">$sql<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"<span style=\"color: #26b31a;\"><span style=\"color: #0100b6; font-weight: bold;\">INSERT INTO<\/span> usertimes (name,date,amount,times)\n<span style=\"color: #0100b6; font-weight: bold;\">VALUES<\/span> (<span style=\"color: #d80800;\">'<span style=\"color: #0206ff; font-style: italic;\">$name<\/span>'<\/span>, CURDATE(), <span style=\"color: #d80800;\">'<span style=\"color: #0206ff; font-style: italic;\">$amount<\/span>'<\/span>, <span style=\"color: #d80800;\">'<span style=\"color: #0206ff; font-style: italic;\">$times<\/span>'<\/span>) <span style=\"color: #0100b6; font-weight: bold;\">ON<\/span> DUPLICATE KEY <span style=\"color: #0100b6; font-weight: bold;\">UPDATE<\/span>    \ndate<span style=\"color: #0100b6; font-weight: bold;\">=<\/span>CURDATE(), amount<span style=\"color: #0100b6; font-weight: bold;\">=<\/span><span style=\"color: #d80800;\">'<span style=\"color: #0206ff; font-style: italic;\">$amount<\/span>'<\/span>, times<span style=\"color: #0100b6; font-weight: bold;\">=<\/span><span style=\"color: #d80800;\">'<span style=\"color: #0206ff; font-style: italic;\">$times<\/span>'<\/span><\/span>\"<\/span>;\n\n<span style=\"color: #0100b6; font-weight: bold;\">if<\/span> (<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>query(<span style=\"color: #0206ff; font-style: italic;\">$sql<\/span>) <span style=\"color: #0100b6; font-weight: bold;\">===<\/span> <span style=\"color: #585cf6; font-style: italic;\">TRUE<\/span>) {\n    <span style=\"color: #3c4c72; font-weight: bold;\">echo<\/span> <span style=\"color: #d80800;\">\"Page saved!\"<\/span>;\n}<span style=\"color: #0100b6; font-weight: bold;\"> else<\/span> {\n    <span style=\"color: #3c4c72; font-weight: bold;\">echo<\/span> <span style=\"color: #d80800;\">\"Error: \"<\/span> <span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #0206ff; font-style: italic;\">$sql<\/span> <span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #d80800;\">\"&lt;br&gt;\"<\/span> <span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>error;\n}\n<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>close();\n?&gt;\n<\/pre>\n<p>At this point we&#8217;re able to write to the database, let&#8217;s see how to read the data.<\/p>\n<h2>Reading from the DataBase<\/h2>\n<p>Just like for the writing, we need a JavaScript function to send the variable to the PHP file and to process the retrieved data.<\/p>\n<pre style=\"background: #fff; color: #000;\">function <span style=\"color: #0000a2; font-weight: bold;\">openUserTimes<\/span>(username) {\n    <span style=\"color: #687687;\">$<\/span>.post(\n        <span style=\"color: #d80800;\">\"returndata.php\"<\/span>,\n        { name: username },\n        function(response) {\n            var myvariable <span style=\"color: #687687;\">=<\/span> response.amount;\n            var times <span style=\"color: #687687;\">=<\/span> response.times;\n\n            <span style=\"font-style: italic;\">console<\/span><span style=\"color: #3c4c72; font-weight: bold;\">.log<\/span>(<span style=\"color: #d80800;\">'Retreived data: '<\/span>, myvariable, times);\n        }, <span style=\"color: #d80800;\">'json'<\/span>\n    );  \n}\n<\/pre>\n<p>The only data we send in this example is the user name for which we want to get the two variables and log them in the console.<\/p>\n<p>The PHP file starts with the connection string, then receives the username posted from the JavaScript. Next is the SQL query which selects everything in the DB table where the name field matches the current username.<br \/>\nFinally we package the returned data into JSON format which can be easily decoded by JavaScript.<\/p>\n<p><strong>The JSON format generated by the PHP:<\/strong><\/p>\n<pre style=\"background: #fff; color: #000;\">{<span style=\"color: #d80800;\">\"name\"<\/span>:<span style=\"color: #d80800;\">\"JohnDoe\"<\/span>,<span style=\"color: #d80800;\">\"date\"<\/span>:<span style=\"color: #d80800;\">\"2017-02-01\"<\/span>,<span style=\"color: #d80800;\">\"amount\"<\/span>:<span style=\"color: #d80800;\">\"4\"<\/span>,<span style=\"color: #d80800;\">\"times\"<\/span>:<span style=\"color: #d80800;\">\"1,2,3,4\"<\/span>}\n<\/pre>\n<p><strong>Contents of returndata.php file:<\/strong><\/p>\n<pre style=\"background: #fff; color: #000;\">&lt;?php\n<span style=\"color: #3c4c72; font-weight: bold;\">header<\/span>(<span style=\"color: #d80800;\">'Content-type: application\/json'<\/span>);\n\n<span style=\"color: #0206ff; font-style: italic;\">$servername<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"localhost\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$username<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"databaseUserName\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$password<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"userPassword\"<\/span>;\n<span style=\"color: #0206ff; font-style: italic;\">$dbname<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">\"databaseName\"<\/span>;\n\n<span style=\"color: #00b418;\">\/\/ Create connection<\/span>\n<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0100b6; font-weight: bold;\">new<\/span> <span style=\"color: #6d79de; font-weight: bold;\">mysqli<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$servername<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$username<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$password<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$dbname<\/span>);\n<span style=\"color: #00b418;\">\/\/ Check connection<\/span>\n<span style=\"color: #0100b6; font-weight: bold;\">if<\/span> (<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>connect_error) {\n<span style=\"color: #0100b6; font-weight: bold;\">    die<\/span>(<span style=\"color: #d80800;\">\"Connection failed: \"<\/span> <span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>connect_error);\n}\n\n<span style=\"color: #0206ff; font-style: italic;\">$name<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">mysqli_real_escape_string<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span>, <span style=\"color: #0206ff; font-style: italic;\">$_POST<\/span>[<span style=\"color: #d80800;\">'name'<\/span>]);\n\n<span style=\"color: #0206ff; font-style: italic;\">$sql<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #d80800;\">'<span style=\"color: #26b31a;\"><span style=\"color: #0100b6; font-weight: bold;\">SELECT<\/span> <span style=\"color: #0100b6; font-weight: bold;\">*<\/span> <span style=\"color: #0100b6; font-weight: bold;\">FROM<\/span> usertimes <span style=\"color: #0100b6; font-weight: bold;\">WHERE<\/span> name <span style=\"color: #0100b6; font-weight: bold;\">=<\/span><span style=\"color: #d80800;\">\"<\/span><\/span>'<\/span><span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #0206ff; font-style: italic;\">$name<\/span><span style=\"color: #0100b6; font-weight: bold;\">.<\/span> <span style=\"color: #d80800;\">'\"'<\/span>;\n\n<span style=\"color: #0206ff; font-style: italic;\">$result<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>query(<span style=\"color: #0206ff; font-style: italic;\">$sql<\/span>);\n<span style=\"color: #0206ff; font-style: italic;\">$response<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">array<\/span>();\n\n<span style=\"color: #0100b6; font-weight: bold;\">if<\/span> (<span style=\"color: #0206ff; font-style: italic;\">$result<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>num_rows <span style=\"color: #0100b6; font-weight: bold;\">&gt;<\/span> <span style=\"color: #cd0000; font-style: italic;\">0<\/span>) {\n<span style=\"color: #0100b6; font-weight: bold;\">    while<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$row<\/span> <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$result<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>fetch_assoc()) {\n        <span style=\"color: #0206ff; font-style: italic;\">$response<\/span>[<span style=\"color: #d80800;\">'name'<\/span>] <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$row<\/span>[<span style=\"color: #d80800;\">\"name\"<\/span>];\n        <span style=\"color: #0206ff; font-style: italic;\">$response<\/span>[<span style=\"color: #d80800;\">'date'<\/span>] <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$row<\/span>[<span style=\"color: #d80800;\">\"date\"<\/span>];\n        <span style=\"color: #0206ff; font-style: italic;\">$response<\/span>[<span style=\"color: #d80800;\">'amount'<\/span>] <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$row<\/span>[<span style=\"color: #d80800;\">\"amount\"<\/span>];\n        <span style=\"color: #0206ff; font-style: italic;\">$response<\/span>[<span style=\"color: #d80800;\">'times'<\/span>] <span style=\"color: #0100b6; font-weight: bold;\">=<\/span> <span style=\"color: #0206ff; font-style: italic;\">$row<\/span>[<span style=\"color: #d80800;\">\"times\"<\/span>];\n    }\n    <span style=\"color: #3c4c72; font-weight: bold;\">echo<\/span> <span style=\"color: #3c4c72; font-weight: bold;\">json_encode<\/span>(<span style=\"color: #0206ff; font-style: italic;\">$response<\/span>);\n}<span style=\"color: #0100b6; font-weight: bold;\"> else<\/span> {\n    <span style=\"color: #3c4c72; font-weight: bold;\">echo<\/span> <span style=\"color: #d80800;\">\"  0 results\"<\/span>;\n}\n<span style=\"color: #0206ff; font-style: italic;\">$conn<\/span><span style=\"color: #0100b6; font-weight: bold;\">-&gt;<\/span>close();     \n?&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration we&#8217;ll go through an example in which we want to store an array for my users. The users can be identified by their unique names. Log in to to your cPanel hosting, find the &hellip; <a href=\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,5],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-articles","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax<\/title>\n<meta name=\"description\" content=\"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax\" \/>\n<meta property=\"og:description\" content=\"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration\" \/>\n<meta property=\"og:url\" content=\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\" \/>\n<meta property=\"og:site_name\" content=\"HTML Online\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/htmlcoding\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-01T18:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-14T08:55:54+00:00\" \/>\n<meta name=\"author\" content=\"HTML Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"HTML Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\"},\"author\":{\"name\":\"HTML Editor\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\"},\"headline\":\"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post\",\"datePublished\":\"2018-07-01T18:07:00+00:00\",\"dateModified\":\"2020-01-14T08:55:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\"},\"wordCount\":600,\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"articleSection\":[\"Articles\",\"JavaScript\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\",\"url\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\",\"name\":\"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/#website\"},\"datePublished\":\"2018-07-01T18:07:00+00:00\",\"dateModified\":\"2020-01-14T08:55:54+00:00\",\"description\":\"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration\",\"breadcrumb\":{\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/html-online.com\/articles\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/html-online.com\/articles\/#website\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"name\":\"HTML Online Articles\",\"description\":\"Tips, tricks, tutorials\u2026\",\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/html-online.com\/articles\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/html-online.com\/articles\/#organization\",\"name\":\"HTML Online\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"contentUrl\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"width\":350,\"height\":350,\"caption\":\"HTML Online\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/htmlcoding\/\",\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\",\"name\":\"HTML Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"caption\":\"HTML Editor\"},\"description\":\"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/@htmlg\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax","description":"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/","og_locale":"en_GB","og_type":"article","og_title":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax","og_description":"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration","og_url":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/","og_site_name":"HTML Online","article_publisher":"https:\/\/www.facebook.com\/htmlcoding\/","article_published_time":"2018-07-01T18:07:00+00:00","article_modified_time":"2020-01-14T08:55:54+00:00","author":"HTML Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"HTML Editor","Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#article","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/"},"author":{"name":"HTML Editor","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d"},"headline":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post","datePublished":"2018-07-01T18:07:00+00:00","dateModified":"2020-01-14T08:55:54+00:00","mainEntityOfPage":{"@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/"},"wordCount":600,"publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"articleSection":["Articles","JavaScript"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/","url":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/","name":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/#website"},"datePublished":"2018-07-01T18:07:00+00:00","dateModified":"2020-01-14T08:55:54+00:00","description":"We will show you the way how to save JavaScript variables to a PHP\/MySQL DataBase easily and securely. In my demonstration","breadcrumb":{"@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/html-online.com\/articles\/javascript-variable-php-mysql-ajax-post-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/html-online.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Save JavaScript variables to PHP\/MySQL DataBase Securely with Ajax Post"}]},{"@type":"WebSite","@id":"https:\/\/html-online.com\/articles\/#website","url":"https:\/\/html-online.com\/articles\/","name":"HTML Online Articles","description":"Tips, tricks, tutorials\u2026","publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/html-online.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/html-online.com\/articles\/#organization","name":"HTML Online","url":"https:\/\/html-online.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","contentUrl":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","width":350,"height":350,"caption":"HTML Online"},"image":{"@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/htmlcoding\/","https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww"]},{"@type":"Person","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d","name":"HTML Editor","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","caption":"HTML Editor"},"description":"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.","sameAs":["https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/@htmlg"]}]}},"_links":{"self":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/comments?post=144"}],"version-history":[{"count":3,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":1146,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/144\/revisions\/1146"}],"wp:attachment":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}