onChange in form_dropdown codeigniter not work - javascript

i try to submit a form without submit button with javascript but not work. My code is:
<?php echo form_dropdown('week_id',$weeklist,'onChange="submitform();"','class ="form-control selectpicker" data-style="btn-danger"'); ?>
and the js is:
<script type="text/javascript">
function submitform() {
document.autosubmit.submit();
}
The name and the id of the form is autosubmit. Also i will try to put the week_id inside the function but not work too. Any suggestions? Thanks in advnace.

If you're going to use an ID:
document.querySelector('#autosubmit').submit();
If you just want to target the form, you need to use the name="" attribute, not the id="" attribute, something like this:
document.forms['autosubmit'].submit();

Related

How to call function after submit form aspx

I want callback alert after submit form, but doesn't worked. Submit not execute.
Only $("#formImpressao").submit(); worked fine.
I try too use $(document).ready(function () { ... :( No Success
Whats wrong?
Sorry for my bad english
<div id="HiddenFormTarget" style="display:none;">
<form id="formImpressao" method="post" target="frmPrint" action="/VisualizarRelatorios/ImprimirRelatorio.aspx""></form>
</div>
<iframe id="frmPrint" name="frmPrint" width="0" height="0" runat="server">
</iframe>
<script language="javascript" type="text/javascript">
$("#formImpressao").submit(function () {
alert('Test');
});
$("#formImpressao").submit();
</script>
$("#formImpressao").submit(function (e) {
e.preventDefault();
alert('Test');
//Insert AJAX to submit form data
});
e.preventDefault() will prevent the default action of the submit event, and then run your alert, but it will NOT submit the form. For that, you will need to use AJAX. It's simple enough to understand, and there are plenty of SO topics on the use of it, so I won't reiterate. But preventDefault() will get you started.
<form id="formImpressao" method="post" target="frmPrint" action="/VisualizarRelatorios/ImprimirRelatorio.aspx""></form>
</div>
Now you can use two ways to submit the form using only jQuery,Recommended method
<script>
$(document).ready(function(){
//instead of id selector use tag selector
$("form").submit(function () {
alert('Test');
});
});
</script>
Another using id selector which is already posted
I reproduce this situation and all right:
[https://codepen.io/piotrazsko/pen/mqMrgo][1]
(1) There appears to be an extra quote in your form tag.
(2) Have you tried your approach without the target attribute in the form tag?
e.g. -
<form id="formImpressao" method="post" action="/VisualizarRelatorios/ImprimirRelatorio.aspx"></form>

javascript submit() command not sending my post data

From this earlier thread I thought I learned that form data could be sent by POST method using javascript submit() command. But I can't get it to work. This demo doesn't make obvious sense with respect to purpose but bear with me. I just wanted to test this specific command which doesn't seem to work for me. Can you please help me? Clicking the regular submit button sends the post data ok but activating the javascript via the link does not.
<html><body>
<?php
$self = $_SERVER['PHP_SELF'];
$posttext = file_get_contents('php://input');
echo "Received input: ".chr(34).$posttext.chr(34)."<br><br>";
echo "<form id='test' action='{$self}' method='post'>";
?>
Please fill in the fields with any text:<br><br>
foo: <input type="text" name="foofield"><br><br>
bar: <input type="text" name="barfield"><br><br>
<input type="submit" value="Submit button works"><br><br>
Submitting by JS not working – why?
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
jQuery(function($) {
$(document).ready(function(){
$("a#submitlink").click(function(){
alert("Yes, the link has been clicked. It is not that.");
$("form#test").submit();
});
});
});
</script>
</body></html>
You need to: event.preventDefault(); the default behaviour from the <a>. The problem is if you don't do that, the page reloads cause that is what the <a> tag does. So even you submit the data you also refresh the page where the submitted data gets lost.
$("a#submitlink").click(function(event){
event.preventDefault();
$("form#test").submit();
});
The other solution is to add # in the href of the <a> tag like:
Submitting by JS not working – why?
this will also prevent the <a> tag from doing a refresh.

Cannot POST form to another php page

It should be quite simple, yet it does not work for me. I need to get user input to form and print it on another php page using GET or POST (because I think it is the easiest option and I am just building a prototype)
My default.php code:
<html>
<head>
<script src="pytimber.js"></script>
</head>
<body>
<p>Enter the parameter</p>
<form method="post" action="php/plot.php">
<input type="text" name='parameter' value="check"/>
<input type="button" value="Plot" class="homebutton" id="plot"
onclick="plot()"/>
</form>
</body>
</html>
Javascript code:
>
function plot() {
alert('JI')
document.location.href = 'php/plot.php';
}
function goBack() {
document.location.href = '../Default.php';
}
Second php page code:
<html>
<head>
<script src="../pytimber.js"></script>
<?PHP
$username = $_POST["parameter"];
print ($username);
?>
</head>
<html/>
1st : Simple remove all your javascript .
2nd : Change the button type to submit
<input type="submit" value="Plot" name="submit" class="homebutton" id="plot" />
3rd : In second page just echo the post variable with isset() function
if(isset( $_POST["parameter"]))
echo $_POST["parameter"];
Actually you don't need the js code, you just need to put a name on your button and change the type to submit and do this to another page:
<?php if(isset($_POST["buttonname"])){
echo $_POST["parameter"]; } ?>
As stated by different people here, the easiest way is to utilise the awesomeness of the submit button, and let HTML take care of the rest.
But, seeing as you use javascript to 'post' the form, let me explain how that could be done as well.
By adding an id to the form, and using that to submit through js, it will work.
Change the form tag and add an id:
<form method="post" action="php/plot.php" id="js_form">
Change the plot function to this:
function plot() {
alert('JI');
document.forms["js_form"].submit();
}
This way the javascript will act as a submit button and send all the data to plot.php
Either
change button type from 'button' to 'submit'
Or
change in js function 'plot'
function plot(){
document.forms[0].submit();
OR
document.getElementById('formid')'.submit;
}

PHP onchange=this.form.submit() on dropdown -> First value get lost

I use Codeigniter Framework and there I use the dorm_dropdown.
<?php echo form_dropdown('wptypes', $wpstatuses, set_value('wptypes'), 'id="wptypes" class="form-select" onchange="this.form.submit()'); ?>
This is my code and when I add the onchange function then the first value of my options get lost. Without onchange everything is working. Why is this so?
Thank you and sorry for my bad english :)
it is because you are missing the closing double quote of the onchange event.
try it like this:
<?php echo form_dropdown('wptypes', $wpstatuses, set_value('wptypes'), 'id="wptypes" class="form-select" onchange="this.form.submit()"'); ?>

how to get a post from a href javascript button

im using a template to make my project and there is a button which is a (a href ) styled button
I want it use it as post in php to pull data .. but its not working . this is the button
<div class="wrapper">
<span class="link1">
<strong>Search</strong>
</span>
</div>
and i want to use it here ...
<?php
if(isset($_POST['submit'])) {
echo '<script type="text/javascript">alert("This button works")</script>';
}
?>
but it does not seem to work ..plz help
Maybe you were referring to javascript instead of Java.
They are not the same.
But yes you could use javascript to submit the form using that anchor.
But first, you need to make sure that you have a form tag inside that markup.
Then bind an event on that anchor:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<script type="text/javascript">alert("This button works")</script>';
}
?>
<div class="wrapper">
<form method="POST" id="your_form_id">
<span class="link1">
<strong>Search</strong>
</span>
</form>
</div>
<script type="text/javascript">
document.getElementById('hide').addEventListener('click', function(e){
e.preventDefault();
document.getElementById('your_form_id').submit();
});
</script>
Simple Demo
FYI: If you want a value to be passed, either utilize hidden input tags. Or put a value attribute and instead use a button.
if(isset($_POST['submit']))
it is false , as you have not submit any form.
Either make it
or add javascript/jquery form submit when clicking on href button.
Hope it helps.
Your May see this

Categories