How to call function through form action with php - php

I want to call a function which deletes my cookies, so I don't get any issues with that.
After deleting the cookies, it should return to my main HTML page.
echo '<tr><td colspan="5"><form method="" action="returnMain()"><button type="Submit">Zurück</button></form></td></tr>';
That's where I want to call my function. So if the button is clicked, it should call the function returnMain, delete the cookies and go back to my HTML page.
How am I able to call the function through php?
At the moment, the only thing that happens is that if I click the button, the function name is added to my URL which doesn't work at all.

Please add form like below code
<form method="" onclick="returnMain()" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Related

I have created one html submit button and connects with php code but, after selecting that how can i go back my html page?

<?php
if($run == TRUE)
echo "Data Inserted Successfully <a href='index.html'>Click here to insert more</a>";
else
echo "Error !";enter code here
?>
I have created one html submit button and connects with php code but, after selecting that how can i go back my html page?
Realistically I would change the form to run the PHP on the same page. So on the line where you create a form, instead of putting <form action='whatever.php'></form> I'd remove the action piece, then move the PHP script onto the same page (make sure it's .php instead of .html), and then wrap your whole script in something like this:
if (isset($_GET['submit'])){ //or post depending on if you're using POST for your form, which is set by doing method=POST (suggested) or method=GET when creating the form's HTML
//your regular php script would go here :)
}
But if you insist on having the PHP on another page and having it direct back you can do it like such:
die(header('Location: index.html')); //change index.html to the directory of the page (or even a URL) that you want them directed to
Best of luck to you!
Use $_SERVER['PHP_SELF'] as action in the form
I would prefer you write your html in php file and then use $_SERVER['PHP_SELF'] this willl submit the data on the same page and you can handle the data of the form being on the same page/php file
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

How to return on the same page after submitting a form in php.?

I am making an e-commerce website where I have lots of products. If a user goes to any product items page and submits any form there then they should come on the same page.
So, how to come on the same page?
On the formular target page set:
header('Location: http://www.example.com/same_page');
Leave action attribute of form blank. Like so:
<form action="" method="post">
Or
<form action="#" method="post">
On your opening form tag add
action="submit.php"
then once it goes to that page when the submit button is hit add this
to the bottom of that php page:
header("Location: success.html OR success.php");
If you want to submit various forms on same page and then go back to the page where the form is submitted, you must also send the form URL of the page where it was sent, preferably in a hidden element. And after processing form redirect to URL stored in hidden.
You can use this :
header('Location: filename.php);
If you get any $_POST errors put it in a condition: if(isset[$_POST])
Thank You All. I Got My Answer
$_SERVER['REQUEST_URI']; will give the current URL with query strings.
Like my page is 'products.php?Product=20'
echo $_SERVER['REQUEST_URI']; =>/products.php?Product=20
So, we can directly use this in header location.

embedded php page after refresh (call self) filling out complete windows

i have an div (id="id_content") in Main.html, and an php file "cmp.php" is dynamically loaded by JQuery.load() in "id_content". It is working fine.
However after the "cmp.php" called itself (for updating) using following code : <form action="<?php echo $_SERVER['PHP_SELF']?>" target="_self" method="post" >
the "cmp.php" occupies the complete browser, what i want ist that "cmp.php" is always being displaye in div "id_content".
Thanks in advance.
The problem is that you are posting the form with the target="_self". This tells the browser that what is returned from the cmd.php should replace the content of the current window.
You have some options:
1) Create an iframe on the main.html like:
<iframe id="postframe"></iframe>
Change the the form to:
<form action="<?php echo $_SERVER['PHP_SELF']?>" target="postframe" method="post" >
The result of cmd.php will now appear inside the iframe when you submit.
2) Use Jquery to post/get your form data and handle the result in Javascript.
<form id="yourform">
</form>
$("#yourform").on("submit",function(e){
//Called when you hit submit
e.preventDefault(); //Don't post the form
//Create a request from form variables and send to server.
//Depending on the result alter id_content.
return false;
});
you can submit form by using Ajax

Adding Google Analytics tracking code to contact form

I'd like to add Google event tracking to my PHP contact form. Doing so requires me to add a particular value to the 'onsubmit' attribute of the element. My PHP file already has an 'onsubmit' attribute defined. When I delete that attribute and enter the required Google code, nothing happens when I click the 'Submit' button (i.e. form does not submit, 'Thank You' page does not load, etc).
Here is the existing PHP code:
<form
class="cpp_form"
name="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"
id="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"
action="<?php $this->get_site_url(); ?>" method="post" enctype="multipart/form-data"
onsubmit="return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">
Here is the Google code I need to enter for onsubmit:
_gaq.push([‘_trackEvent’, ‘button’, ’clicked’, ’contact us’,, ’true’])
Any ideas on how to do this/what I'm doing wrong?
I've also tried entering the Google code as the value for the "onclick" attribute. When I do that, the form can successfully be submitted, but it does not show up as an 'event' in Google Analytics.
Couple of things:
1) The GA code you posted shows smart-quotes. I'm not sure whether that's just a c/p thing posting here or if that's what you used in your actual code, but smart-quotes is invalid javascript syntax.
2) There are several ways you can do this. One way is to add it to your onsubmit as you tried. You don't need to replace the current stuff in there. Just add it to it. Since your current thing in there is returning something, you will want to add it before:
onsubmit="_gaq.push(['_trackEvent', 'button', 'clicked', 'contact us',,'true']); return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">
Alternatively, you can look for where that xxx_pform_doValidate_xxx() function is defined and put your GA code in there.
3) To be clear, you are tracking that the form submit button was clicked. In my experience, tracking form submit button clicks has little value, since it does not indicate that the form was successfully submitted, nor does it tell you why submission failed if it failed.
You could add the google code to your validation function or use a separate function where you first put the google code and then return the value of your validation function. Or add it inline before the return statement...
But as you have a "Thank You" page, you could also track that instead although that will not give you the client- and server-side unvalidated submissions.

I need help getting a <div> to refresh on form submit

I have looked all over stack, and googled the crap out of it, but everytime I come across something I only come across snippets and I cannot find the full code, how can I get a div to reload on form submit?
this is the div I want to refresh
<div id="accsettings">
<?php require_once('profileform.php'); ?>
</div>
this is the form
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?username=' . $user_name; ?>">
Thanks
I need it to refresh on submit so that it shows the affect the player had on their profile
(by showing their comment and that they increased their rep on the webpage by +1 and removing the textfields/submit form, all that is already taken care of, I just need it to refresh on submit)
What's the name of you submit button? If it were submit, you could do something like:
<div id="accsettings">
<?php if (!isset($_POST['submit'])) { require_once('profileform.php');} else {echo '&nbsp';} ?>
</div>
This assumes that the form submits to the current page.
$("form").on('submit', function (e) {
$.post($(this).attr('action'), $(this).serialize() + '&submit=TRUE').done(
function () {
$("#accsettings").load('profileform.php');
}
);
e.preventDefault();
});
What this will do is an ajax load of profileform.php into the contents of the #accsettings div. This assumes that your form submission properly updates the output of profileform.php and is done via ajax (I see no evidence of that in your code currently, though).

Categories