Is it possible to send data from a webpage but only certain parts of it? For example:

Webpage from form:

<html>
<head>
</head>

<body>
<div>Non-form data</div>
<form method="post" action="">
<input type="text" id="field1" />
</form>
<div>Other non-form data</div>

<?php
$field = mysql_real_escape_string($_POST["field1"]);

// Database info

echo $field . "is in the database or whatever...";
?>
</body>
</html>

I want to check the value of field1 against a database in PHP. When I do $.post($(this).attr("action"), { field: $(#field1).val() }, function(data) { alert(data); }); it returns the entire coding from the webpage instead of just the echo part of the php, which is what I want only. Is there a fix for this or do I have to actually create another file with the php coding and send it to that? Thanks for any help I can get.

You can either create another file(my suggestion) or insert and IF at the begining of the page.

When you make an HTTP Request to an webpage it will return all things that were wrote in the response. This mean that any html tags that are not handled by the server-side script(php in this case) will be returned as well.

In my opnion the best option is to have a page(or more if you have lot of methods) only for AJAX methods.

I think I should've mentioned that the php code is suppose to run after keyup or blur (which I have coding for in jquery already), not after form submit. Let's say I want to run this code:

<?php

$check = mysql_real_escape_string($_POST["field1"]);

$search = mysql_query("SELECT * FROM table WHERE thisfield='" . $check . "'");

$returnrows = mysql_num_rows($search);

if ($returnrows == 0)
    echo "Not in database.";
else
    echo "Is in database.";

?>

What if statement do I need to put in the form page to execute the above PHP code on field1 blur or keyup and how do I put it in jquery?

$(document).ready(function()
{
    $("#field1").bind("blur keyup", function()
    {
        $.post($(this).attr("action"), { field: $("#field1").val() }, function(data)
        {
            //What changes do I need in the above line?
            alert(data); //The resulting code should return 'Not in database.' or 'Is in database.' only and nothing else.
        }
        );
    }
    );
}
);

You need to get the action from the Form element, something like this:

var $input = $(this),
    value = $input.value,
    $form = $input.parents("form"),
    action = $form.attr("action");

    alert(value); // Test the value from the input
    alert($form.length); // Test if the form was found ( bigger then 0 means it was )
    alert(action); // Test if action was found

$.post(action, { field: value }, function(data)
{
    alert(data); //The resulting code should return 'Not in database.' or 'Is in database.' only and nothing else.
}

Well unfortunately I couldn't get it to work. I had to resort to creating a PHP page and pointing the URL to that page, even though I didn't want that. Oh well, thanks anyways.

Were you able to find the form element?

The form element was found, which was done already by my second post. I just couldn't get the php code to execute by itself without the other code shown in the form page so I had to create a page with just that php code.

Good for you. It's the best implementation! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.