I am currently working on a php/html/javascript project. I have a form where when the user presses the submit button, it should run a javascript function and then afterwards post to a php page to process the data that was submitted in the form.
I have the form run a javascript method as below
<form class="form" id="addImageForm" name="addImageForm" action="javascript:validateAddImage();" method="post">
The method validates the form, calls another method and then it submits the form using document.myForm.submit();
How do I get it to submit the form to another php page to process the data including upload selected files.
Thanks for any help you can provide.
Set yout 'action' parameter to your PHP script, and do any javascript procesing in a javascript event.
<form class="form" id="addImageForm" name="addImageForm" action="processing.php" method="post" onsubmit="return validateAddImage();">
Then in your js validation, return 'true' if everything's fine, or 'false' if not. Returning 'true' means 'continue with the submit process and send over data to processing.php
What you need is onsubmit.
<form class="form" id="addImageForm" name="addImageForm" action="addImage.php" onsubmit="return validateAddImage();" method="post">
Replace the action attribute with the name of your PHP page, and have the validateAddImage method as an onsubmit event.
Change the action attribute to the address of the php script you're posting to.
Call the javascript function in an onsubmit attribute instead of action. But you'll have to prefix it with a return statement.
<form class="form" method="post" action="your/script.php" id="addImageForm" name="addImageForm" onsubmit=return javascript:validateAddImage()>
That way if validateAddImage() returns false, the form won't be submitted. But if it returns true it will.
Related
I have a script which is run with a parameter (e.g. details.php?studentid=10325).
On this script I have a form with the following form code so that the form data is sent to the current script. However, what's happening is that the script is running without the parameter. How do I preserve the parameter in this form code?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Not really recommended, but you could add a hidden <input> tag with the studentid so it is sent back when you submit the form. Like so :
<input type="hidden" name="studentid" value="<?= $_GET['studentid'] ?>">
Leave the action blank, and the form will submit back to the current url
<form method="post" action="">
Keep in mind you have specified post method, so the form values will come thru in $_POST, whereas your studentid on the query string will be in $_GET['studentid'], you can work around that by using $_REQUEST['studentid'] instead, but make sure you don't have a field in the form also called studentid
I am working with PHP forms.
The opening form tag looks like this:
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
This generated the following HTML:
<form name="adv_search_form" action="index.php?&rel=common_listing&module=company&sort_option=&search_option=&show_max_row=15" method="GET">
When I submit this form, I land on a URL:
http://localhost/projectcode12may2014/ampanel/index.php?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
But what I want is that
?field%5B%5D=broker_name&adv_operation%5B%5D=LIKE&value%5B%5D=&query_type%5B%5D=AND&submit=Submit
should be appended to the action URL on the landing page. Can anyone suggest any good method for doing this?
Thanks in advance.
If you are using method="get" the parameters in action are overwritten. Only what is in the from will be transmitted.
You need to add hidden fields for all these values. For example:
<input type="hidden" name="module" value="company" />
That is your from data.
You have method="get" in your form tag. This means that the form data is concatinated to the URL. Use method="post" to have send form data as a POST request and keep your URI clean.
So change this line
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="GET">
to this
<form name="adv_search_form" action="<?php echo $targetpage; ?>" method="post">
Then you can access your form data in PHP with the global $_POST var which holds an array with the submitted data.
This is a normal behavior. Using GET method discard all parameters specified directly in action attribute.
Use POST method instead or move parameters from action attribute of the form to input fields with type="hidden".
I have a form in one of my pages and I want to make it submit to the same page. I am doing this:
<form method="POST" action=".">
But when I submit the form it submits to the root directory of my site instead of to the same page the form is on.
I know that the form is being submitted because I tried temporarily changing the method to GET and when I submitted the form, the URL showed the get variables.
Why is the form not submitting to the current page?
If it's relevant, I'm using PHP with xampp.
Try this:
<form method="POST" action="">
This is what I do
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
I'm a newbie though so if Juampi's answer works too , I'd go with it.
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>
I would like to use the data entered in the html form and insert into MYSQL. I have a separate php (cus.php). but nothing is happening with current code I have
at the moment when I click "register" I'm nav to the php file. Thank you
You forgot the most important thing about an HTML form: the <form> tag.
You have to wrap the whole form (all inputs) which should be submitted when clicking like this:
<form method="POST" action="cus.php">
...
</form>
This will send all inputs to cus.php and make them available there as variables $_POST['input_name']. You can alternatively use GET as the method (and then use the $_GET array instead).
Edit: Didn't see it, you actually do have a form tag. However it's missing the target file in its action attribute.
First, see DCoder's comment. It's the most important.
Change:
<form action="" method="seller">
To:
<form action="cus.php" method="post">
Does that fix it?
Try to do this
FORM action="your script" method="Post"