简体   繁体   中英

Popup message and ajax to jquery form

I create a form with PHP and jQuery, I add this in footer of my website, all I need is to display the form results in a popup in the main of my website not in the footer and make this to work without page refresh (with ajax). I add here my entire code:

<form enctype="multipart/form-data" action="" method="post">
   <input name="url" type="text"/>
    <input type="submit" name="submit" value="Check" class="btn btn-primary"/><br/>
</form>

<?php
if(isset($_REQUEST['submit'])) {
    $url = $_REQUEST['url'];
if(substr( $string_n, 0, 7 ) != "http://" && substr( $string_n, 0, 8 ) != "https://")
$url = "http://".$url;
if(stripos($url,"folder/")>0)
$content = file_get_contents($url);
else
$content = file_get_contents($url."/folder/");
if(preg_match("/Copyright ver\. ([0-9.]+)/", $content, $result))
echo "Your year is : ".$result[1];
else
echo "Sorry cannot determine year";
}
?>

EDIT

dont forget to put jquery in top of the html page

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

Check below solution

//this will be your html file

<form id="form1" enctype="multipart/form-data" action="" method="post">
    <input name="url" type="text"/>
    <input type="submit" name="submit" value="Check" class="btn btn-primary"/><br/>
</form>
<script>

    $(function() {
    $('#form1').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
        type: 'post',
        url: 'post.php',
        data: $('form').serialize(),
        success: function(html) {
            alert(html);
        }
        });
    });
    });
</script>

create a post.php file put your php code

<?php
if (isset($_REQUEST['submit']))
{
    $url = $_REQUEST['url'];
    if (substr($string_n, 0, 7) != "http://" && substr($string_n, 0, 8) != "https://")
        $url = "http://" . $url;
    if (stripos($url, "folder/") > 0)
        $content = file_get_contents($url);
    else
        $content = file_get_contents($url . "/folder/");
    if (preg_match("/Copyright ver\. ([0-9.]+)/", $content, $result))
        echo "Your year is : " . $result[1];
    else
        echo "Sorry cannot determine year";
}
?>

If you are only using the code provided, you are still missing some AJAX code. As you have used both the jQuery and JavaScript tags, I wasn't sure if you are using Vanilla or jQuery (my example uses jQuery).

My example will make an AJAX request when the user clicks the submit button. $your_url will need to be set to a file which contains your PHP code you specified in the question.

 $('#my_button').on('click', function(e) { e.preventDefault(); $.ajax({ type: "POST", data: { text: $('#some_text').val(), }, url: "<?php echo $your_url; ?>", success: function(data) { console.log($data); } }); });
 <form enctype="multipart/form-data" action="" method="post"> <input name="url" type="text" / id="some_text"> <input type="submit" name="submit" value="Check" class="btn btn-primary" id="my_button" /> </form>

Your PHP will need to be changed so that it works correctly with this AJAX request. As mentioned in the comments, it seems like you do not fully understand how AJAX works, and how it communicates with PHP. I suggest the following documentation and tutorials:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM