How can I integrate an HTML form with a PHP script to update a value in mySQL?
How should I place the scripts for them to work together? Any guidelines for best practice?
I am failing to integrate the work. I tried everything I could think of but I am not able to make it work here is a list of what I tried so far:
Writing the script in functions.php which caused a fatal error.
Creating a new file named index1.php, still not integrating with the HTML form.
Putting index1.php in the form action, but still didn't work.
Something weird, as shown in the image, but still didn't work.
Form:
<div style="width:100%; margin:0 auto;background-color: #f8f8f8;">
<form action="index1.php" method="post">
<fieldset>
<div style="padding 10px;text-align:left;"> <textarea name="value" value="12"
placeholder="meow" style="padding: 5px;background-color: #f8f8f8"></textarea></div>
<div style="padding: 10px; text-align: right;">
<input type="submit" name="ins" value="Update" style="padding: 8px; background-color: #328269">
</div>
</fieldset>
</form>
</div>
PHP Script:
<?php
if (isset($_POST['ins'])) {
global $wpdb;
$data_array = array(
'value' => $_POST['value']
);
$table_name = 'P100';
$rowResult = $wpdb->update($table_name, $data_array);
if ($rowResult > 0) {
echo "Data Added Successfully!";
} else {
echo 'Error Occurred. Please try again.';
}
$wpdb->flush();
}
?>
My aim is to embed the form in a popup so that I can allow quick editing of the front end from the Popup when I display all my text in shortcodes retrieved from the database.
Related
This question already exists:
PHP form validation not working as expected [duplicate]
Closed 3 years ago.
I'm a bit new to php and I was going over a tutorial where they are making validation form, but when I try and replicate what they do I don't get the same as them, when they run the code, the input box shows up red because the user hasn't placed anything in the input box. When I try it, nothing happens, the input box doesn't show me any errors.
But when I try and submit my form with data it just sends me to the next page, and if I dont type anything I just stay in the same page.
All this code is inside index.php
if(($_POST['email']) != "")
{
header('Location:final.php');
}
else{
$emailError = "validation";
}
<form method="post" action="index.php" >
<!--Get warning here when no input is placed-->
<div class="<?=$emailError?>">
<label>E-mail Address:</label>
<input type="text" name="email">
</div>
The red border is the following code:
.validation {
border: 1px solid red;
}
You can include it with your code like this
<style>
.validation {
border: 1px solid red;
}
</style>
<form method="post" action="index.php" >
<!--Get warning here when no input is placed-->
<div class="<?=$emailError?>">
<label>E-mail Address:</label>
<input type="text" name="email">
</div>
I am very new to html and php and need a little help. This is actually my first post on stackoverflow so I apologize if the content isn't done right.
I am asking for a username and password from a user and then I want to take that information and use it in a script that is run on the server via PHP.
The problem I'm having is that the first time a user goes to the html and fills out the form, the $_POST in the php file doesn't receive anything. I know that because I echoed the variables in the php file. But after the user presses the 'Back' button on the php page and returns back to the html, when going through it again, everything works fine.
So the code works, it just doesn't work the first time. Any ideas?
HTML:
<body>
<div class="loader" id="container" style="visibility:hidden;"></div>
<form method="post" action="Login_Page.php">
<b>Username:</b><input type="text" placeholder="University ID" name="userid" autofocus/>
<b>Password:</b><input type="password" placeholder="Password" name="pswrd"/>
<input type="submit" class="button" value="Submit" onclick="showDiv()"/>
</form>
</div>
</body>
CSS:
.loader {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url('https://cdn.shopify.com/s/files/1/1247/2733/t/4/assets/loading.gif?11916921113420493983') 50% 50% no-repeat rgba(255, 255, 255, 0.3);
filter: alpha(opacity=60);
}
Script:
<script type="text/javascript">
showDiv = function() {
document.getElementById("container").style.visibility="visible";
}
</script>
PHP:
<?php
$username = $_POST["userid"];
$pswrd = $_POST["pswrd"];
echo $username;
echo $pswrd;
$cmd = "path/to/script.sh $username $pswrd";
exec($cmd, $output, $return);
if ($return != "0") {
echo '<h1><b>Login was unsuccessful. Please try again.</b></h1>';
echo '<form>';
echo '<input type="button" value="Back" onClick="history.go(-1);return true;"/>';
echo '</form>';
} else {
echo '<h1><b>Login was Successful.</b></h1>';
}
?>
Could be the same problem as there: Onclick javascript stops form submit in Chrome.
It's caused by your onclick function at form submit.
So it ended up being a security certificate issue. I was trying to open up my website on an iPad and I didn't have the correct security certificate download on the iPad. This might sound weird but it has been consistently working after I did that. Thank you, everyone, for all of your helpful comments and assistance.
I have a WYSIWYG tool where I can create a content. At the bottom, I have a form where I can submit a value (from an input text) to a database. That works fine. Now, I am trying to submit the content that has been created in the WYSIWYG in the database.
I am thinking of using a value in the input as shown below:
<input name="videoLink" type="text" value="John" required/>
and use javascript to make the value dynamic. But there must be an easier way. To make the form submit the content of a div instead of having to type anything in the input box.
My code is shown below:
angular.module("textAngularTest", ['textAngular']);
function wysiwygeditor($scope) {
$scope.orightml = '<h2>Put Your Text Here</h2>';
$scope.htmlcontent = $scope.orightml;
$scope.disabled = false;
};
.ta-editor {
min-height: 80px;
height: auto;
overflow: auto;
font-family: inherit;
font-size: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular-sanitize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/textAngular/1.1.2/textAngular.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.min.css" rel="stylesheet" />
<div ng-app="textAngularTest" ng-controller="wysiwygeditor" class="container app">
<h3>WYSIWYG Editor</h3>
<div text-angular="text-angular" name="htmlcontent" ng-model="htmlcontent" ta-disabled='disabled'></div>
<!--<h3>Raw HTML in a text area</h3>
<textarea ng-model="htmlcontent" style="width: 100%"></textarea>-->
<h3>Preview</h3>
<div ng-bind-html="htmlcontent"></div>
<!--<h3>Bound with ta-bind, our internal html-binding directive</h3>
<div ta-bind="text" ng-model="htmlcontent" ta-readonly='disabled'></div>-->
<button type="button" ng-click="htmlcontent = orightml">Reset</button>
<form action="Insert.php" method='POST' enctype='multipart/form-data'>
<label><input name="videoLink" type="text" required/></label>
<input id="button" type="submit" name="log">
</form>
</div>
Use placeholder instead of value.
Set value =" " but the placeholder="Put text here" ... otherwise you could get a lot of Johns.. I presume that's what you want to avoid? I don't think you can avoid php/ passing values to php from html [using javascript] to enter values into a database.
Your form isn't that big. You don't need that amount of js unless your site is using angular/is included in all pages of the CMS. So if the question is really how to pass variables to php with minimal javascript, then comment.
You should still use placeholder instead of value. Otherwise if people don't change the text / maybe just press enter/submit.. your required error message won't fire. That's a lot of Johns in the db! :)
Hope this helps
I have a relatively simple sounding question that hopefully entails a simple answer. I am currently developing a website that is a scroll-down type; everything is on one page, you know the one. My 'contact' section is at the very bottom of the page, and I am of course doing form validation with PHP. The validation part works, I have no trouble with that. However, if validation fails, the browser takes me back all the way back to the top of the page; this is inconvenient for obvious reasons.
I am aware of the 'header()' function, which I can use to keep me at the bottom of the page if an error occurs. As an example, at the top of my HTML page before the DOCTYPE, I can write:
if ($errors) {
header(Location: 'somelocation.php#contact');
}
This works, but for some reason it prevents my PHP embedded in my html to work. If some errors occur, I want to display them using PHP on the page:
<h2>Contact</h2>
<?php if($errors) { ?>
<p class="warning">Please fix the errors</p>
<?php } ?>
'Please fix the errors' does not appear. It does appear however, if I remove the header function from the page, so I know the problem is related. So basically, if I remove the header() function, the page goes back to the top, and the errors show; if I keep the header() function, the page correctly stays where it is, but no errors show.
Alternatively, this question also can be asked in the case of, what happens when the form is validated correctly, the 'header' function is called, and I want to stay at the bottom of the page and display some HTML saying 'Thanks, your form has been submitted'? (I don't want to go to a 'thank-you' page or anything, just stay in the same spot and give a 'thanks' on the page) I assume I'd run into the same problem. Is there a solution to this?
Thanks in advance!
Specifying a location in the header will cause the browser to redirect to that URI. This is being called before any output is sent to the browser.
When the page is reset, $errors == false, so the paragraph isn't displayed.
It sounds like you need to integrate AJAX, or a mixture of server- and client-side code here.
A very simple and efficient way to validation of a form is using jquery, A perfect example of this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<em>*</em><input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<em> </em><input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
</body>
</html>
Happy Coding.!!!
<div class = 'buttons'>
<button type="submit" class="regular" name="save">
<img src="elephant.png" alt=""/>
Memory
</button>
</div>
This is the code for a submit button
however when i try validate the form using php, the button does not seem to be submitting
<?php if( isset($_POST['save']) && $_POST['save'])
{
$submit = $_POST['save'];
echo "lol";
}
else
{
echo "lola";
}
Submit buttons only work in forms. Try to wrap the whole thing in a form tag and try again.
Your submit button doesn't have any value to send when posting the form. The <button> element does not send its element's content as its submit value. You still have to add a value attribute to the element in order for it to have a submitted value. Since you don't, it's sending an empty string, which is causing your conditional statement to fail:
if( isset($_POST['save']) && $_POST['save'])
Since $_POST['save'] is empty, that second part returns false and thus it goes to your else block.
Here is the code that you want:
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="buttons">
<button type="submit" class="regular" name="save" value="Save">
<img src="elephant.png" alt="Elephant" />
Memory
</button>
</div>
</form>
As written already your code lacks the wrapping. While submit button as a button might work without the form, then catching the _POST data surely doesnt.
Also, your solution for image-button could be better, maybe something like this:
<style type="text/css">
input[type="submit"] {background: url('elephant.png') right no-repeat; padding: 10px; padding-left: 30px; font-size: 14px; font-weight: bold;} /* or call this .regular */
</style>
<?
if ($_POST['save']) { // the trigger
echo '<h1>Cooking with fire now!</h1>';
}
?>
<form method="POST" action="">
<div class="buttons">
<input type="submit" name="save" value="Memory" class="regular" />
</div>
</form>
Note that class = "buttons" with the spaces, is incorrect syntax!
You should include your code within <form method="post" action="path/to/your/php/processing/file"> and </form> tags
Replace your button code with <input type="submit" class="regular" name="save" />
Remove isset($_POST['save']) && part from condition of if as you don't have set value for your button.