-2

*Updated with full code example, after reading comments I figured I'd post example code that I'm still struggling with. Basically I'm hoping clicking on the link in the html with submit the form to called.php and return it's value into the div on the html page.

    <!DOCTYPE html>
<head>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>

        function dSubmit(){

            $.ajax({
                type: "POST",
                url: "called.php",
                data: $("#projectAddition").serialize(),    

                success: function(data) {
                    //here id where you want display
                    $('#id1').val(data.var1); //here is value1
                }
            }

}

        </script>

</head>

<body>

    <form id="projectAddition" action="#" method="POST">
    <input type="text" name="projectName">
    </form>
    <a href="#" onclick="dSubmit()">Submit Form</a>
    <br>Output<br>
    <div id="id1"></div>

</body>

PHP:

$name = $_POST['projectName'];
// Do something with variable

$array = array(
    'var1'=> $name,
    'var2'=>'value2'
);
echo json_encode($array);
5
  • use your data object to access your returned data, and in your quickprojectadd.php file make sure it returns your data Commented Sep 4, 2016 at 0:46
  • Thanks for the help, I'm aware of the theory I'm just struggling putting into place. Any example code would be very helpful as everything I have tried seems to result in underfined. Commented Sep 4, 2016 at 1:00
  • 2
    Similar answer can be found here stackoverflow.com/questions/2410773/… Commented Sep 4, 2016 at 1:10
  • I have read the similar answer before posting along with a variety of other questions similar but I'm still struggling with it. Commented Sep 4, 2016 at 17:56
  • Updated non working example above. Commented Sep 4, 2016 at 18:03

2 Answers 2

0

first put your variables into array like that

$array = array(
    'var1'=>'value1',
    'var2'=>'value2'
);
echo json_encode($array);

and then

in ajax you get in this way

$.ajax({
    type: "POST",
    url: "url",
    data: $("#projectAddition").serialize(),    
    success: function(data) {
        //here id where you want display
        $('#id1').val(data.var1); //here is value1
        $('#id2').val(data.var2); //here is value2
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you help, I've now updated the original question to contain the full code as it's still not functioning correctly. If you can help me further that would be great!
0

Well with more tinkering it appears the working HTML is as follows:

// url, formName passed in through function

$.ajax({
       type: "POST",
       url: url,
       data: $("#" + formName).serialize(), // serializes the form's elements.
       success: function(data)
       {
                $("#AtoZ").append('<div id="success">' + data + '</div>');}

PHP Side:

echo $return="Project Added, Reference: $id";

Just wondering if not using JSON is a problem? The only thing I'm passing is a string e.g. 'failed'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.