javascript check function for a html form - php

I'm having a problem with a HTML form (name, e-mail, subject, message), I can't validate the form if it was completed correct or not. I want to call a javascript function when the form is submitted and alert if a field wasn't completed correct.
My problem is with the submit button, because my boss said that I must use he's own php function to insert a submit button. The php function looke like:
function normal_button($text, $width, $param = '', $class = 'button-blue')
{
$content = '<div class="'.$class.'" style="width:'.$width.'px;" '.$param.'>'.$text.'</div>';
return $content;
}
and how I call the function:
echo normal_button('Send it', '100', 'onclick="document.getElementById(\'emailForm\').submit();" ');
I tried to call the js function declaring the onSubmit="return checkForm(this)" in the <form action='email.php' method='post'> HTML form, but I can't "catch" the submit. Also I tried to catch it with jquery, $(document).ready(function() { $('.normal_button').click(function() { }); ); but I can't return false if a field wasn't completed correct.
I must use he's PHP function to insert the submit button, but how can I "catch" the fields, how can I validate them? Any suggestions? Thank you !

First of all, set ID for that form and for sbmit DIV button.
<form action='email.php' method='post' id='mailForm'>
and
echo normal_button('Send it', '100', 'id="mailFormSubmit"');
then do check with jQuery:
$('#mailFormSubmit').click(function(){
// fields check code goes here
....
// if all fields are OK, then submit form
$('#mailForm').submit();
});

Related

Call js function with image Button with php without submit

Im coding php and i want to use image-button to call function.
$del= "<input type='image' src='delete.png' name='delete_cat' value='{$cat_name}' onclick='delete_exe(this.form,this.name,this.value)' >";
This button is into a submit form
<form method="post" action="caffe_menu_category_post.php">
/form>
My problem is that when js function(delete_exe()) call and execute, then have auto-submit.
I don't want that. Is there any solution or alternative code?
Can i set submit off for my button?
Thanks in advance.
ps in my code i submit form with submit input
input type = "submit" value = "Submit Your Entries">
Use
return false;
in the end of your delete_exe(); function. That will cancel submitting.
Ok. i find the solution. I change my post form
form name="myForm" method="post" action="caffe_menu_category_post.php" onsubmit="return validateForm()">
And use javascripts
var val=true;
function delete_exe(sel,kind,name){
//Set val false to stop submit
val=false;
}
function validateForm(){
if(!val){
//Stop submit
return false;
}
}
So the auto-submit stop.
On the oher hand when click submit button i call js function to set val=true;
i don't know if these is the best solution, but it's work for me!

Sending SCROLLTOP javascript via Submit

I'm trying to submit a form after updating the value of a hidden field from a javascript variable.
This is the code:
<form name=form1 id=form1 method=get action=gestionale.php>
...
...
<input type=hidden name=scrolltop id=scrolltop value=''>
<input type=button name=update value=Update onClick=vai('form1');>
</form>
<script>
function vai(formid) {
document.getElementById('scrolltop').value=document.getElementById('offerte').scrollTop;
document.getElementById(formid).submit();
}
</script>
The form submit works correctly but $_GET[scrolltop] after form submission, is empty even if it was filled with javascript. And testing it with alert before submit shows the correct value.
Anyone knows why?
Thanks a lot.
In your script...
The line document.getElementById('scrolltop').value = document.getElementById('offerte').scrollTop;
What exactly do you intend to do with the last bit (('offerte').scrollTop)?
As far as I understand, you want to return a text value, however setting the value to this will not return anything.
You will need to parse the query string ($_GET variable) and assign that to a variable which you will then pass as the value for the hidden field.
If I'm being completely in the dark here, I'm sorry. You should include a bit more of your code or additional information if my answer seems far fetched.
Anyway, I hope this helps!
I found this:
function submit_form(formid) {
var hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = "theName";
hidden.value = document.getElementById('offerte').scrollTop;
var f = document.getElementById("form2");
f.appendChild(hidden);
f.submit();
}
I submit the form using a javascript function instead of submit button.
Here I can set a hidden filed to a value I need in my action page.

How to pass the value from my form to another form?

Hello i cannot seem to pass the value from my form to another form .
To explain this i have 3 forms:
alldeals.php
stardealdesc.php
form.php
what i have have done is pass the values from alldeals.php to stardealdesc.php and it work fine. however in stardealdesc.php i have a button that when click will popup a form (form.php)
i wan to make use the values that is parse from alldeals.php to stardealdesc.php to be also parse to formp.php .
This is the pop up codes:
function Popup() {
window.open( 'form.php', "myWindow",
"status = 1, height = 500, width = 500, resizable = 0" )
}
<form>
<input type="button" onClick="Popup()" value="Buy">
</form>
And this is the statement to get the values from alldeals.php
$cmeter = $_REQUEST['cmeter'];
which work fine but i want to take this value and display it in form.php
I believe you mean PASS not PARSE,
You need to store the saved form inputs into the session
session_start();
$_SESSION['form1_values'] = $_POST; // Submitted from form 1
$_SESSION['form2_values'] = $_POST; // Submitted from form 2
$_SESSION['form3_values'] = $_POST; // Submitted from form 3
Then you can refer to those values from any PHP script at any time until the session is cleared
not sure about security terms, but if you want to pass the details on form.php that is displaying in pop window, then use that in url like form.php?username=somename

help storing postdata with codeigniter

Here is my scenario, I am creating a form with codeigniter, I understand how to populate the fields with models and such. I have the layout of the form. It is now running from my index function. I want to store all the data given to that form and access them in a postdata array with each index being the name of the value. Please help. CodeIgniter, PHP
you create the form
echo form_open('mycontroller/mymethod');
// rest of form functions
or <form name="myform" method="post" action="<?php echo site_url('mycontroler/mymethod');?>" > // rest of html form
then, in Mycontroller:
function mymethod()
{
$postarray = $this->input->post();
// you can pass it to a model to do the elaboration , see below
$this->myloadedmodel->elaborate_form($postarray)
}
Model:
function elaborate_form($postarray)
{
foreach($postarray as $field)
{
\\ do your stuff
}
}
If you want XSS filtering you can pass a TRUE as second parameter in the $this->input->post() call. Check user guide on input library
See code igniter's input class
One exemple of how to form your code would be:
public function add_something()
{
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') { // if the form was submitted
$form = $this->input->post();
// <input name="field1" value="value1 ... => $form['field1'] = 'value1'
// you should do some checks here on the fields to make sure they each fits what you were expecting (validation, filtering ...)
// deal with your $form array, for exemple insert the content in the database
if ($it_worked) {
// redirect to the next page, so that F5/reload won't cause a resubmit
header('Location: next.php');
exit; // make sure it stops here
}
// something went wrong, add whatever you need to your view so they can display the error status on the form
}
// display the form
}
This way your form will be displayed and if submitted its content will be processed, if an error occurs you will be able to keep the submitted values to pre-enter them in the form, display an error message etc ... And if it works the user is redirected to that he can reload the page safely without submitting multiple times.

Send $.post from JQuery to controller in code igniter and load the result

I want to send a post from HTML that contains info about a certain form to a controller in code igniter. The I want the controller to process the info and loads a certain page inside a div. Here is my code. I think I'm supposed to use something like .html or something? I'm not quite sure, I dont understand it
The controller
function search_friend(){
//this function gets text from text field and searches for a user and returns all users similar to this dude
// $this->input->post($searchFriendForm);
// $this->input->post($searchFriendText);
$this->load->model('userProfile_m');
$people = $this->userProfile_m->get_user_by_name($this->input->post($searchFriendText));
$this->load->view('addFriendSearchResult',$people);
}
the form in html
<form method="post" action="" name="searchFriendForm" id="add-friend-search">
<input type="text"/ name="searchFriendText">
<input type="button" class="small green button" id="add-friend-button" />
</form>
the jquery function
$("#add-friend-button").click(function(){ //start click
$.post("<?php echo site_url('userProfile/search_friend'); ?>", $("#add-friend-search").serialize());
$("#insert-activity").load("<?php echo base_url().''?>system/application/views/addFriendSearchResult.php");
$("#add-friend-search").slideUp("slow",function(){});
}); //end click
Firstly, in your controller change this line like this (u need to pass the string name of the field here):
$people = $this->userProfile_m->get_user_by_name($this->input->post('searchFriendText'));
Next, change your jQuery to be like this:
$("#add-friend-button").click(function(){ //start click
$.post("<?php echo site_url('userProfile/search_friend'); ?>",
$("#add-friend-search").serialize(),
function(data){
$("#insert-activity").html(data);
});
$("#add-friend-search").slideUp("slow",function(){});
}); //end click
You cant call your view directly, and you don't need to. The post should return the data, which you can write out to your #insert-activity element.

Categories