<script language="javascript" type="text/javascript">
function popitup(url) {
newwindow=window.open(url,'name','height=200,width=350');
if (window.focus) {newwindow.focus()}
return false;
}
</script>
Link to popup
the top code is my index, where in you can see a Link to a popup link
and when you click that a simple form will rise, the form has 2 textbox and one submit button this is the code for the form popupex.php
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
print"<form action='func.php?add/inside_add' method='post'>";
?>
<input type='text' name='ok'><br>
<input type='text' name='notok'><br>
<input type='submit'>
</form>
</body>
</html>
Question: why the code in my func.php is not running? simply because of error? and can some explain the error and also give solution?
this is my func.php
<?php
function goback()
{
window.history.go(-1);
}
class add{
function inside_add(){
goback();
}
}
?>
what im hoping in this function , is when i click the button in my popupex.php it will go back in the form popupex.php. thx
The code you provided is a javascript function window.history.go(-1)
use header('Location:' . $_SERVER['HTTP_REFERER']); for PHP to return to the previous page.
Related
I am trying to run this simple Ajax Post example like:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("test.php",
{
name:"Hello Ajax"
},
function(data){
$("p").html(data);
}
);
});
});
</script>
</head>
<body>
<p></p>
</body>
</html>
and PHP (test.php) as:
<?php
$name = $_POST['name'];
echo '<a>'.$name.'</a>';
?>
No error message but not getting any result back! can you please let me know what I am doing wrong?
Just a simple change, added button element in your markup. Click it and it should work:
<body>
<button>Click Me</button>
<p></p>
</body>
Your javascript is looking for the button element on which when clicked, will trigger the ajaxcall in test.php
$("button").click(function(){
By clicking the button, it will display Hello Ajax, the echo from the test.php and the one that you've send.
$.post("test.php",
{
name:"Hello Ajax"
},
I have a website where users leave a comment which will be inserted into database.
After that the user will be redirected to the same page with updated and inserted comment display.
I used php and mysql for this. It is working fine.
But for safer side, I inserted html code in textbox and it is executing.
My html code is:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick=myFunction(Harry Potter,Wizard)>Try it</button>
<script>
function myFunction(name,job)
{
alert(Welcome + name +, the + job);
}
</script>
</body>
</html>
In my page try button is coming with an alert message.
I don't have any idea how to handle this.
Can any one help me?
Use quotes. jsfiddle
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick='myFunction("Harry Potter","Wizard")'>Try it</button>
<script>
function myFunction(name,job)
{
alert("Welcome" + name +", the" + job);
}
</script>
</body>
</html>
I have an HTML form that currently just posts the data directly to a PHP file. I want to update the code so that the submit button sends the data to a JavaScript function so that I can create an AJAX function. Is it possible for the submit button to activate a JavaScript function rather than posting to a php file? The only thing I have come up with is below, which quite obviously does not work:
<html>
<head>
<script type="text/javascript">
function ajax(){
//...
}
</script>
</head>
<body>
<form action="ajax();">
<!-- ... -->
<input type="submit" value="Submit" />
</form>
</body>
</html>
You can give the "submit" input a "click" handler that explicitly prevents the default behavior from being carried out.
<input type='submit' value='Submit' onclick='ajax(event)'>
Then in the function:
function ajax(event) {
if ('preventDefault' in event) event.preventDefault();
event.returnValue = false; // for IE before IE9
// ...
}
edit #Esailija points out correctly that another option is to handle the "submit" event on the <form> element instead. The function would look pretty much the same, in fact exactly the same, but you'd wire it up like this:
<form id='yourForm' onsubmit='ajax(event)'>
That will also trap things like the "Enter" key action, etc.
Of course you can. But it's more useful to call your Javascript function in the input like this :
<input type="submit" value="Submit" onclick="ajax();" />
And remove the action part in the form.
I jQuery you can use event.preventDefault(); otherwise just use return false;
http://jsfiddle.net/mKQmR/
http://jsfiddle.net/mKQmR/1/
Pointy is correct... just add a click handler to the submit button, however make sure the last line of the click handler returns "false" to prevent the form from actually being posted to the form's action.
<html>
<head>
<script type="text/javascript">
function ajax(){
//...
return false;
}
</script>
</head>
<body>
<form action="thispage.htm">
<!-- ... -->
<input type="submit" value="Submit" onclick="ajax();" />
</form>
</body>
</html>
Let's say after my user fills a form and clicks the submit button, I want the user redirected to the same page with the form and a jquery animation will play. The header function is there to prevent the user from refreshing the page and resubmitting the same data.
UPDATE:
I've decided to use $_SESSION to record the user's session to try to get the jquery animation to play after they get redirected to the same page:
<?php
session_start();
if (isset ($_POST['submit']))
{
$connect = mysql_connect("","","") or die("Error connecting to db");
mysql_select_db("") or die("Error connecting to db");
$text = $_POST['text'];
mysql_query ("INSERT INTO header VALUES('','$text')");
$_SESSION['jq']=='a';
header('Location: header.php');
die();
}
if($_SESSION['jq']=='a') {
$_SESSION['jq']='';
echo'
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").hide(3000);
});
</script>';
}
?>
<html>
<head>
<style type="text/css">
#jquery {border:1px solid black;width:300px;}
</style>
</head>
<body>
<form name="header" action="header.php" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<div id="jquery">this is jquery animation</div>
</body>
</html>
Use like this
if (isset ($_POST['submit']))
{
$text = $_POST['text'];
mysql_query ("INSERT INTO header VALUES('','$text')");
//redirect to same page that user submitted form from
header('Location: form.php?jq=a');
die();
}
if($_GET['jq']=='a') {
//play animation
echo'<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//insert jquery animation code
});
</script>';
}
Or
session_start();
if (isset ($_POST['submit']))
{
$text = $_POST['text'];
mysql_query ("INSERT INTO header VALUES('','$text')");
//redirect to same page that user submitted form from
//$_SESSION['jq']=='a'; commented for your understanding remove this in your code.
$_SESSION['jq']='a';
header('Location: form.php');
die();
}
if($_SESSION['jq']=='a') {
$_SESSION['jq']='';
//play animation
echo'<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//insert jquery animation code
});
</script>';
}
Can't you just keep the user on the same page, and process the form submit on the same page (by setting the action attribute of the form to the page that the form is on)?
That would deal with the redirection problem. You can then have your jQuery animation code in the original page, and start the animation in a if(isset($_POST['submit'])) statement.
You can use the jQuery form plugin.
There are similar questions at SO, but none that seem to address this.
Below is a very simplified variant of my situation. Drupal/PHP site -- I have a form w/ submit button that I am using jquery.form plugin to ajax-ly submit. It works fine if I use the submit (#submit) button.
Now, I want to programmatically fire that button using another button (#submit2) outside of the form. I can do that using jquery click() function, but the content coming back isn't going to the ajax target as I would expect.
I do not have much freedom to re-organize this code, else i would.
(Note I tried to make this code easy for you to run by src-ing jquery and the form plugin from my website.)
Ideas? Thanks!
<?php
if ($_REQUEST['submit'] == 'Submit') {
print 'ajax returns ... ' . $_REQUEST['text'];
exit;
}
?>
<html>
<head>
<script type="text/javascript" src="http://enjoy3d.com/scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="http://enjoy3d.com/scripts/jquery.form.js"></script>
<script type="text/javascript">
$(function() {
$('#form').ajaxForm( { target: $('#span') } );
$('#submit2').click( function() { $('#submit').click(); } );
});
</script>
</head>
<body>
<span id='span'>target span</span>
<form method='post' id='form'>
<input type='text' name='text' size='50' />
<input type='submit' id='submit' name='submit' value='Submit'/>
</form>
<input type='submit' id='submit2' name='submit2' value='Submit Too?' />
</body>
</html>
I managed to solve a similar situation to yours. If the only objective of simulating a click on submit1 is to submit the form, you might try:
$('#submit2').click(function() {
$('#form').trigger('submit');
});
You may also need to return false immediately after triggering the form submit button from the non-form submit button click event code. For example:
<script type="text/javascript">
$(function() {
$('#form').ajaxForm({ target: $('#span') });
$('#submit2').click(function() { $('#submit').click(); return false; });
});
</script>
It works for me. Is that what you are looking for?
Have you tried giving a name to the form, and instead of
$('#submit2').click( function() { $('#submit').click(); } );
doing
$('#submit2').click( function() { document.myForm.submit(); } );
That should do the same thing as having the submit button clicked if the form has been ajaxified.