{"id":10699,"date":"2022-09-24T16:15:12","date_gmt":"2022-09-24T10:45:12","guid":{"rendered":"http:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/"},"modified":"2022-09-24T16:15:12","modified_gmt":"2022-09-24T10:45:12","slug":"solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/","title":{"rendered":"[Solved] how to save data from textboxes to database on submit button click using php"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-36811045\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"36811045\" data-parentid=\"36810923\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The html code below is a snippet of how your form should look, though you have mentioned you already have this part done:<\/p>\n<p>page1.html<\/p>\n<pre><code>&lt;form method=\"POST\" action=\"page2.php\"&gt;\n  &lt;input type=\"text\" name=\"usernameForm\"&gt;\n  &lt;input type=\"password\" name=\"passwordForm\"&gt;\n  &lt;input type=\"submit\" value=\"Submit\"&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n<p>The php code below then obtains the variables from page1.html after a user Submits their information from the form, and then inserts a line into a table in the database. When using this code, don&#8217;t forget to replace all the table names, columns, etc with the ones you have on your form, as well as the 4 &#8220;********&#8221; variables that give your code access to your database:<\/p>\n<p>page2.php<\/p>\n<pre><code>\/\/uses MySQL (PDO)\n&lt;?php\n$usernameForm = $_POST['usernameForm'];\n$passwordForm = $_POST['passwordForm'];\n\n$servername = \"********\";\n$username = \"********\";\n$password = \"********\";\n$myDB = \"********\";\n\ntry {\n    $conn = new PDO(\"mysql:host=$servername;dbname=$myDB\", $username, $password);\n    $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n    echo \"Connected successfully\"; \n\n    $sql = \"INSERT INTO myTable (username, password) VALUES (:usernameForm, :passwordForm)\";\n    $stmt = $conn-&gt;prepare($sql);\n    $stmt-&gt;bindParam(':usernameForm', $usernameForm);\n    $stmt-&gt;bindParam(':passwordForm', $passwordForm);\n    $stmt-&gt;execute();\n\n    }\ncatch(PDOException $e)\n    {\n    echo \"Connection failed: \" . $e-&gt;getMessage();\n    }\n\n$conn = NULL;\n?&gt;\n<\/code><\/pre>\n<hr>\n<p><strong>Edited:<\/strong> After having read your comment on wanting to have all this code on one page, a simple way of doing it is the following:<\/p>\n<p>Replace this line:<\/p>\n<pre><code>&lt;form method=\"POST\" action=\"page2.php\"&gt;\n<\/code><\/pre>\n<p>with this line:<\/p>\n<pre><code>&lt;form method=\"POST\" action=\"&lt;?php echo htmlspecialchars($_SERVER[\"PHP_SELF\"]); ?&gt;\"&gt;\n<\/code><\/pre>\n<p>What is happening here is the following:<br \/>\nPHP code is always run on the server. When you have your website fully loaded, your connection with the server terminated. Whatever information you are sending to the server via the <code>&lt;form&gt;...&lt;\/form&gt;<\/code> tags need to establish a connection with the server, which is why in general people user two pages (one for the input, and another one that receives the data), <strong>HOWEVER<\/strong> there is a simple and common way of keeping all this code in one page: have the <code>&lt;form&gt;...&lt;\/form&gt;<\/code> send to itself!<\/p>\n<p>To do this you have 2 options: <\/p>\n<ol>\n<li>if your page was called page1.php, then you could just replace <code>action=\"page2.php\"<\/code> with <code>action=\"page1.php\"<\/code>. However this in general is not good programming because if for some reason the name of your page changes, then your code breaks, which leads us to the second (and better) option.<\/li>\n<li>replacing <code>action=\"page2.php\"<\/code> with <code>action=\"&lt;?php echo htmlspecialchars($_SERVER[\"PHP_SELF\"]); ?&gt;\"<\/code>. What this does is almost the same as option 1, HOWEVER the php code inside <code>action<\/code> is asking the $_SERVER superglobal for what the name of the page is, hence preventing your page from &#8220;breaking&#8221; in cases like when you rename your file.<\/li>\n<\/ol>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved how to save data from textboxes to database on submit button click using php <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The html code below is a snippet of how your form should look, though you have mentioned you already have this part done: page1.html &lt;form method=&#8221;POST&#8221; action=&#8221;page2.php&#8221;&gt; &lt;input type=&#8221;text&#8221; name=&#8221;usernameForm&#8221;&gt; &lt;input type=&#8221;password&#8221; name=&#8221;passwordForm&#8221;&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt; &lt;\/form&gt; The php code below then obtains the variables from page1.html after a user Submits their information from &#8230; <a title=\"[Solved] how to save data from textboxes to database on submit button click using php\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\" aria-label=\"More on [Solved] how to save data from textboxes to database on submit button click using php\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,1117,339],"class_list":["post-10699","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-mysqli","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] how to save data from textboxes to database on submit button click using php - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] how to save data from textboxes to database on submit button click using php - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The html code below is a snippet of how your form should look, though you have mentioned you already have this part done: page1.html &lt;form method=&quot;POST&quot; action=&quot;page2.php&quot;&gt; &lt;input type=&quot;text&quot; name=&quot;usernameForm&quot;&gt; &lt;input type=&quot;password&quot; name=&quot;passwordForm&quot;&gt; &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt; &lt;\/form&gt; The php code below then obtains the variables from page1.html after a user Submits their information from ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-24T10:45:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] how to save data from textboxes to database on submit button click using php\",\"datePublished\":\"2022-09-24T10:45:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\"},\"wordCount\":339,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"html\",\"mysqli\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\",\"name\":\"[Solved] how to save data from textboxes to database on submit button click using php - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-24T10:45:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] how to save data from textboxes to database on submit button click using php\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] how to save data from textboxes to database on submit button click using php - JassWeb","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:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] how to save data from textboxes to database on submit button click using php - JassWeb","og_description":"[ad_1] The html code below is a snippet of how your form should look, though you have mentioned you already have this part done: page1.html &lt;form method=\"POST\" action=\"page2.php\"&gt; &lt;input type=\"text\" name=\"usernameForm\"&gt; &lt;input type=\"password\" name=\"passwordForm\"&gt; &lt;input type=\"submit\" value=\"Submit\"&gt; &lt;\/form&gt; The php code below then obtains the variables from page1.html after a user Submits their information from ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/","og_site_name":"JassWeb","article_published_time":"2022-09-24T10:45:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] how to save data from textboxes to database on submit button click using php","datePublished":"2022-09-24T10:45:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/"},"wordCount":339,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","mysqli","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/","name":"[Solved] how to save data from textboxes to database on submit button click using php - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-24T10:45:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-save-data-from-textboxes-to-database-on-submit-button-click-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] how to save data from textboxes to database on submit button click using php"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10699"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10699\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}