1

I want to post input values without form submit. I have this link , in which an onClick() function is declared.

 <a href="javascript:void(0);" id = "graph" onClick="validation('graph','D');" >Link</a>

I am restricted in my project to avoid to us FORM SUBMIT technique.I have to choose some other way to post the values and get Database updated. What to do instead of Form Post ??

NewUser
  • 3,471
  • 7
  • 48
  • 74
Rahil
  • 271
  • 1
  • 4
  • 13
  • 2
    The keyword you are looking for is Ajax. Google has plenty of resources on the subject. (Incidentally, doing this without a form is a terrible idea as it prevents you from making your JS [unobtrusive](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript) and [progressive](http://en.wikipedia.org/wiki/Progressive_enhancement) and doesn't allow you to use the nice form-specific DOM APIs that are available to JS in a browser). – Quentin Mar 04 '13 at 17:36
  • Where are those `input` values? Do you have a form? Please post the whole code, because either you need AJAX or just simple JavaScript. Also - irrelevant to your question though, avoid using `onClick` and write `onclick` instead (all characters in lower case). – Dimitris Damilos Mar 04 '13 at 17:38
  • Actually, my whole project is on one php page, and i cannot use any form action to jump page to any other php page – Rahil Mar 04 '13 at 17:42

2 Answers2

0

You need to use AJAX to do so. Even you can use onblur() function for text field when the field gets deactivated automatically a JavaScript function is called in which you can put the code to update the database.

$.ajax({
type: 'POST',
url: 'http://xyz.com/updatedb.php',
data: { 
    'foo': 'bar'
}
});

This code will send the data form your form.

In the updatedb.php file use this code:

<?php
 $in = $_POST['foo'];
 $con = //connection string;
 $query = "insert into tablename values('$in')";
 $result = mysqli_query($con, $query)
   or die('Something is wrong in query chek it');
 ?>
NewUser
  • 3,471
  • 7
  • 48
  • 74
  • Do you have any sample of all doing this AJAX > JSON and all that ??please attach any sample – Rahil Mar 04 '13 at 17:43
0

If you are talking about a form strictly,and if you can use a javascript framework, such as jQuery, it would gives you all the AJAX functionalities your are looking for, in no time:

See http://api.jquery.com

You can check this question: jQuery AJAX submit form

Which gives you a way to do it. You could also improve your code to make it more accessible and less obstructive (which makes it a lot more easier to support, since javascript would be separated from your html code).

 <button id = "graph" >my action</a>

and in your head, in a script element:

$(document).ready(function() {
    // the functions below are executes once the document is loaded
    $("#graph").click(function(){
        validation('graph','D');
    });

});

If you do not talk strictly about a form, you can do it by defining uri and handler: http://www.domain.com/?r=my-action/arg1/arg2/arg3...

There are commons techniques you will find in a Model View Controller frameworks (I do not no which server langage you are working with).

Note: there are others framework than jQuery, but I am used to it http://en.wikipedia.org/wiki/Comparison_of_JavaScript_frameworks

Community
  • 1
  • 1
Bertrand
  • 368
  • 4
  • 13