0

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.

2
Contributors
7
Replies
8
Views
4 Years
Discussion Span
Last Post by AleMonteiro
0

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.

0

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.
        }
        );
    }
    );
}
);

Edited by secretply

0

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.
}
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.