Form Cross domain POST request using PHP

后端 未结 3 1706
悲&欢浪女
悲&欢浪女 2021-01-03 10:40

I am trying to send data from a form to a php file so I can store it in a database, but its not working...

The code for the form is not on the same server as the php

相关标签:
3条回答
  • 2021-01-03 11:10

    Your input element <input type="text" id="name" value="" /> must set up the name attribute as name="name".

    form #cname after edited

    <form id="cname" align="left" action="post">
        <label for="name">Enter Name:</label>
        <input type="text" id="name" name="name" value=""  />
        <input type="submit" value="Submit" data-inline="true">
    </form>  
    

    You can gather more informations from the jQuery API Documention:
    http://api.jquery.com/
    http://api.jquery.com/serialize/

    0 讨论(0)
  • 2021-01-03 11:14

    Add this at the beginning of your PHP file:

    header("access-control-allow-origin: *");
    

    More info on cross domain policy here.

    0 讨论(0)
  • 2021-01-03 11:18

    I think you need to prevent the default function of the submit button using .preventDefault() because as I look on your code you want to submit your form using ajax

     $("#cname").submit(function (e) {
        e.preventDefault();
        $.ajax({
            url: 'http://www.clubbedin.isadcharity.org/createclub.php',
            crossDomain: true, //set as a cross domain requests
            type: 'post',
            data: $("#cname").serialize(),
            success: function (data) {
                $("#result").html(data);
                alert("Data " + data);
            },
        });
    });
    

    and please use .ajax() so that you can set your ajax request to a cross-domain request

    http://api.jquery.com/jQuery.ajax/

    0 讨论(0)
提交回复
热议问题