<form> is not working with mootools - php

I have following code in my php file
<?php
if(isset($_POST['updatebtn']))
{
mysql_query("update table......");
}
?>
<script type="text/javascript">
window.addEvent('domready', function(){
new FormCheck('myform');
});
</script>
<form name="myform" method="post" id="myform" action="">
<input type="text" class="validate['required']" />
<input type="submit" value="updatebtn" />
</form>
as you can see i have added mootools form validation in my file. mootools form validation work only if i add id="myform" in my form. but if i add that (i. e. id="myform"), it is creating problem, means it is not executing update query that i have written at top.
if i remove id="myform" from my form tag, it is executing that update query
do anyone have any idea??

The javascript would validate the input and prevent the form to submitting so it's normal that your server side script won't run.
I think you should make a difference between things run on server side, such as PHP and things run on client side, i.e. javascript.

Related

Polymer iron-form - paper-button submission not working

I am trying to use the Polymer element Iron-Form to submit information into the $_POST array. However my submit button (a paper-button) - which should run the script to submit the form - does not seem to submit the form when pressed.
I'm new to Polymer and to PHP, so I'm not sure what is going wrong.
Form script
<form is="iron-form" method="post" id="insert-project-form" action="/form/handler">
<paper-input label="Project Title" name="title"></paper-input>
<paper-input label="Client ID" name="clientid"></paper-input>
<paper-input label="Working Hours" name="workhours"></paper-input>
<paper-button raised onclick="submitForm()">Submit</paper-button>
<script>
function submitForm() {
document.getElementById('insert-project-form').submit();
}
</script>
</form>
I have been having the same problem, and have been doing it the same way you do it. According to the Documentation it should work. But I have found a work around for this problem
Add a normal button for the submission and style its visibility to hidden
<button type="submit" id="SubmitButton" name="submit" style="visibility:hidden;"></button>
And in your javascript code change the submitForm function to this
function submitForm(){
document.getElementById('SubmitButton').click();
console.log("Submitted!")
}
And keep the paper button line the way it is.
<paper-button raised onclick="submitForm()">Submit</paper-button>
What it does is when the paper button is clicked, it triggers a click event on the normal submit. I'm pretty sure there are more efficient ways than this, but I will be using this for now.
<form is="iron-form" method="post" id="insert-project-form" action="/form/handler">
the attribute at the very end "action" needs to pass it to your php file. Assuming your php file is in a folder called "php". the solution to this would be
<form is="iron-form" method="post" id="insert-project-form" action="php/yourphpfile.php">
and your logic would be contained in the php file that submits it to a database if needed.

how to get value from a form before submit

i want to get the values from a form before its action redirect it.
for example in this form, i want to grab the "text_one" and send it to database before it be redirected to google. I also want "text_one" in google too.what should i do?
<form method="post" action="google.com">
<input type="text" name="text_one">
<input type="submit">
</form>
try this ...
<form method="post" onsubmit="return getdata()" action="google.com">
<input type="text" name="text_one" id="text_one">
<input type="submit">
</form>
<script>
function getdata(){
var txtOne = document.getElementById('text_one').value;
// Do Something
}
</script>
You can change the action to "yourscript.php" and do s.th. like:
<?php //yourscript.php
//save $_POST['text_one'] to Database
header('Location: http://google.com');
?>
Or you can call the "yourscript.php" with ajax to do it in the background.
Try this :
echo (isset($_POST['text_one']) ? $_POST['text_one'] : '');
or Use Ajax Ajax is the answer of your question
For a pure PHP solution, you can work with the idea presented by #v.eigler. In order to create a POST request to a Google server (or what ever server you want), you just need to use some library to make the HTTP request, I strongly recommend you to take a look at the Guzzle library.
Using this should be easy enough, you just need to redirect the form handling to a script that you own, do your own processing and then create an HTTP post to the real destination.
I do it with little complexity.
I change your form action handler.
<form method="post" action="yourscript.php">
<input type="text" name="text_one">
<input type="submit">
</form>
Now, Its time to handle it server side. I have put comments for explanation.
<!-- yourscript.php -->
<?php
echo $_POST['text_one'];
// also do required server side operation.
?>
<!-- note: Action part is google.com -->
<form id="myform" method="post" action="google.com">
<!-- Note: value of input already set.-->
<input type="text" value=<?php echo $_POST['text_one'];" ?> name="text_one">
<input type="submit">
</form>
<script language="JavaScript">
// submit your form as soon as page loaded.
document.myform.submit();
</script>

Populating a div with php file that processes a form

I have a link on a website when it's clicked it will load a form in a div. The form is processed by a PHP script. I need the output from the PHP script to appear in that div. Any idea how to do this? Thanks
File header.php
<html>
<head>
<script>
$(document).ready(function(){
$('#mylink').click(function(){
$('#content').load("form.php");
});
});
</script>
</head>
<body>
<li>Add Account</li>
file index.php
<?php
include_once 'header.php';
?>
<div id="content"></div>
in form.php
<form id= method="POST" action="script.php">
......
........
<input type="submit" value="Add account" />
</form>
in script.php
i need to send error message or thank you note in div #content
Ajax may help you. It's easy to do so. Try it.
First, this sounds like a job that JavaScript would be perfect for. But if you want to go old school, once you submit your form, depending if you set it to be method="post" or method="get" in the form tag, you can access it with php in the receiving script with $_GET or $_POST.
example:
html:
<form action="yourScript.php" method="post">
<input type="text" name="location" />
php (file: yourScript.php): echo $_POST['location'];

stop jquery to run server side code on validation fail

Sorry for the very basic question but i am asking here after trying to search alot.
Basically i am doing client side validation its running the half code but it is running the php code also even if it fails.
<script type="text/javascript">
function formValidation(){
if($("#inputCname").val() == ''){
alert("Category missing");
($this).css("border-color","red");
return false;
}
return true;
};
</script>
Below i am calling the function. Please note that i have already tried with onclick event on the submit button still not working.
<form class="form form-search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" onsubmit="formValidation();">
Thanks alot.
You have to add a return to the onsubmit to stop the submission
<form class="form form-search" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" onsubmit="return formValidation();">
EDIT
There's also a problem in your JS function
$("#inputCname").css("border-color","red");
Instead of doing this for every form element, use jquery validation, It will do it all for you. Also showing alert for every form element is not a good practice. It will solve your all problems with validation providing you more features.
You can download jquery validation and check out its documentation from following link.
Download link http://jqueryvalidation.org/
Documentation to implement http://jqueryvalidation.org/documentation/

Should Javascript Input to HTML form onSubmit with # action to PHP $_GET (all code on same file) work in theory?

I have the code request a date to be chosen by the user through this code:
<script type="text/javascript" charset="utf-8">
var k3 = new Kalendae(document.getElementById("cal"), {
months:3,
format:'DD-MM-YYYY',
mode:'single'
});
</script>
The user selects a date through the javascript and hits an html submit button:
<form name="input_data" action="#" method="post"
onSubmit="javascript:location.href="#?date_value=" + k3.getSelected();">
This code's html action directs it to reload the same page with # so that php can capture it with:
$dateValue= $_GET['date_value'];
If I echo $dateValue should it echo the original javascript input in theory? If no, how would it need to be modified?
Simply using
<form action="" method="post">
should work.
Say you call your button "Save" (name="save"). This will make trigger the PHP on submit.
if (isset($_POST['save'])) { // Your stuff here
You should instead update your calandar information on change, storing it in a hidden input-field. I know submitting a form with a hash-tag is causing problems in several browsers.
If you remove the # from the form tag like so:
<form name="input_data" action="#" method="post"
onSubmit="javascript:location.href="?date_value=" + k3.getSelected();">
The url will be appended ?date_value=Whatever and called, but the form submits and grab the action and post the data to it and if you keep it the page won't be called, so theoretically no cause the get would be lost because of the second page load with not get value, but you can get this working by using this editing to your form like so:
<script type="text/javascript">
function edit() {
document.input_data.action = "?date_value="+k3.getSelected();
}
</script>
<body>
<form name="input_data" action="" method="post" onSubmit="javascript:edit();">
...
</form>
</body>
And this will send the get value alongside the post values from the form and you can do it without the need to another function by updating the onSubmit attribute in the form and delete the edit function like so:
<body>
<form name="input_data" action="" method="post" onSubmit="javascript:this.action='?date_value'+k3.getSelected();">
...
</form>
</body>

Categories