I was just trying to submit a simple form to the same page but when it is submitted it will call PHP function on the same page. However I was trying to do some JavaScript validation before submission. So I want to know what the difference between using onSubmit call js function in the form tag and onClick call js function with button.... This is what I am currently trying to do.
<?php
function tobecalled()
{
echo "This was run";
}
?>
<html>
<head><title>Testing</title>
<script type="text/javascript">
function testResults (form)
{
var TestVar = form.inputboxname.value;
if(TestVar == '')
return false;
else
return true;
}
</script>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" onSubmit="return testResults(this);">
<input type="text" name="inputboxname" />
<input type="submit" value="Save" name="submit" />
<?php
if(isset($_POST['submit']))
tobecalled();
?>
</form>
</body
</html>
It works..
But if I make (Submit Via JS)
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
...
<input type="submit" value="Save" name="submit" onClick="return testResults(this);"/>
...
Its still calls the PHP function tobecalled()--Why? I am expecting it not call. How do it work?
The reason that it is allowing it to go through is because you are passing this in the onclick event. In this instance this is referring to the submit button not the form as required by the function.
Thus form.inputboxname.value returns undefined which is not '' (empty string) and therefore the testResults function returns true. So the submit is then activated.
The difference is this. this points to a different object in onClick than in onSubmit. Your function expects a form to be passed, but when you use onClick, you give it the submit button. That's why the second method doesn't work as expected.
Because regardless of whether you add your javascript to the onsubmit of the form or the onclick of the submit button the form will still be submitted by the submit button. That means that a request is being sent back to the server and $_POST['submit'] will be set. Since that variable is set you find your function being called.
Related
I have created a form that has a subit button which will submit the for to a database, and a email button which will run a .cgi script to process the mail. Im having trouble getting the submit button to work.
here is the form buttons
<FORM name="drop_list" method="POST" >
<input name="emailForm" type="button" id="emailForm" onClick="sendFormEmail()" value="Email">
<input name="add_patient" type="button" id="add_patient" onClick="addPatient()" value="Add Patient">
</FORM>
And here is my javascript
function sendFormEmail() //email form
{
alert ("Email, this is disabled atm");
document.drop_list.action = "html_form_send.php"
document.drop_list.submit(); // Submit the page
return true;
}
function addPatient() //Post form to data base
{
alert ("Post to database");
document.drop_list.action = <?php echo $editFormAction; ?>;
document.drop_list.submit(); // Submit the page
return true;
}
The sendFormEmail() works fine, but when I try to use addPatient(). The form will not submit and something in the document.drop_list.action = ; line completely breaks the java script.
Thanks in advance for your help.
-Gregg
You haven't shown the value of $editFormAction, but I'll bet it doesn't have quotes around it. So you need to write:
document.drop_list.action = "<?php echo $editFormAction; ?>";
If you checked in the Javascript console, you should have seen an error message about an undefined variable or undefined not having a php method. And then if you looked at the JS source, you would have noticed that the script name doesn't have quotes around it, like it does in sendFormEmail.
Here is my javascript and php code.
js
function validate()
{
myform.submit();
}
php
<form action="http://myurl/" method="post" name="myform" target="_blank">
<input type="id" name="txtid" value="3">
<input type="button" value="Submit" onClick="validate()">
</form>
How can I pass the value of txtid to the form action so that my new window URL would have http://myurl/3. Any idea how to trick this?
Thank you..
Try this
function validate()
{
//grabs the value of txtid
txtid = $('#txtid').val();
//uses var to redirect to the correct URL
window.open(http://myurl/"+txtid);
}
For example: I want to accept or reject a photo.
The input button for accept and the form is:
<form method="post" action="viewRequests.php">
<input type="submit" onClick="showAlertBox()">
</form>
showAlertBox() is a javascript function which shows a confirmation dialog with Yes or No. If I choose No then the form submission should not be triggered.
what to do in this case
Your Javascript function should return false if you do not want the form submission to be triggered.
Eg:
<script type="text/javascript">
function showAlertBox() {
var answer = confirm ("Some yes/no question to the user");
return answer; // either true or false
}
</script>
<form action="submit.php">
<input type="submit" onClick="return showAlertBox()">
</form>
you can do this way:
onsubmit="return showAlertBox();"
the showAlertBox function could be like that:
function showAlertBox()
{
return confirm('validate'); // confirm returns TRUE on "OK"
}
maybe very easy!
I'm php coder and I don't have experience in js but I must do this for one of my codes
suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<?php
//some code
if(array_key_exists('update',$_POST)){
//somecode
}
?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
</form>
</body>
</html>
show as function name does not really make sense here (imo), but you could do:
<input type="submit" name="sub" value="sub1" onclick="show(this)">
and
function show(element) {
element.value = 'sub2';
}
Important:
But that will actually not solve your problem. As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page. So every change you made the current page is lost anyway.
The question is: What are you trying to do?
It seems to me that you should change the value of the button on the server side. You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.
Update:
I see several possibilities to solve this:
You could keep using JavaScript and send and get the data via Ajax. As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.
You could add a GET parameter in your URL with which you can know which label to show for the button. Example:
<form method="post" action="?update=1">
and
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
Similar to 2, but use a session variable (and not a GET parameter) to keep track of the state.
Update2:
As you are already having $_POST['update'] you don't need the URL parameter. It could just be:
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
</form>
</body>
</html>
This should do it
function show(){
document.getElementsByName('sub')[0].value = 'sub2';
return false;
}
Edit: if you don't want it to submit the form, just add a return false, but then you'd need to change your onclick from your submit button to your forms onsubmit;
<html>
<head>
<title>test</title>
<script>
function show()
{
document.getElementById("sub").value= "sub2";
return true;
}
</script>
</head>
<body>
<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>
Hiya:
i know some people would be so tired of my questions, but I'm working on a uni project and need to get it done as soon as possible. This question is about using JS on a button(button) and sending a php_my_sql update on the same button. The problem is JS uses button, right? but PHP uses button(submit). How can I get these two to work on one of these buttons, cuz there has to be only one button.
this is my code for JS
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex)
}
</script>
HTML
<form method="post">
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="**submit/button**" onclick="formAction()" name="Collect" value="Collect" /></form>
PHP
<?
if (isset($_POST['Collect'])) {
mysql_query("UPDATE Player SET score = score+10
WHERE name = 'Rob Jackson' AND rank = 'Lieutenant'");
}
?>
This can be a way
Submit the form through JS after removing parameter
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect")
x.remove(x.selectedIndex);
document.forms[0].submit();
}
</script>
Input type button
<input type="button" onclick="formAction()" name="Collect" value="Collect" />
Embed jQuery and use $.post() to send an AJAX request.
JavaScript can interact with the button whilst the user is navigating the page and entering data into the form. The instant the user pushes the submit button and the request for the form submission is sent JS no longer has control. The request is sent to the form's action (most likely a PHP file) which processes the request and gives an answer back.
If you really need to combine the two, look into AJAX.
<?php print_r($_POST); ?>
<script type="text/javascript">
function formAction(){
var x=document.getElementById("collect");
x.remove(x.selectedIndex);
submit_form();
}
function submit_form() {
document.form1.submit();
}
</script>
<form method="post" name='form1'>
<input type='hidden' name='Collect'/>
<select id="collect" name="Select1" style="width: 193px">
<option>guns</option>
<option>knife</option>
</select> <input type="button" onclick="formAction()" name="Collect" value="Collect" /></form>
<?
if (isset($_POST['Collect'])) {
//do whatever update you want
}
?>
Simple Solution
Make this modification in the form tag
<form method="post" onsubmit="return formAction()">
In JavaScript function add a line "return true;" at the end of the function.
Voila ..!!! you are done..!!
Enjoy..!!