April 25, 2012

Seach.php is not working and not showing any results

Question by Erik

I’m trying to figue out how to make my search.php script work with mySQL. I can’t get the information to show up. Not sure where the problem is.

PAGE 1:

<form action="search_result.php" method="GET">
    <input type="text" name="reg" />
    <input type="submit" value="Search" />
</form>

PAGE 2:

<?php
$host="localhost";
$username="XXXXXXXXXXX";
$password="XXXXXXXXXXX";
$db_name="XXXXXXXXXXXX";
$tbl_name="reg_add";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$record = $_POST['record']; // if coming from e.g. a form
$result=mysql_query(" SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");


$row = mysql_fetch_assoc($result);
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];
?>

<input  name="reg" value="<? echo "$record" ?>">

<input  name="first_name" value="<? echo "$first_name" ?>">

<input  name="last_name" value="<? echo "$last_name" ?>">

Answer by Adriaan

You form is method GET and in your PHP you use this:

$record = $_POST['record']; // if coming from e.g. a form

How are you gonna get the POST[‘record’] if your form has the method GET?

I guess you should or change your form to:

method="POST" 

or change your $record in php to:

$record = $_GET['record'];

Try this version:

You form:

    <form action="search_result.php" method="POST">
    <input type="text" name="reg" id="reg" />
    <input type="submit" name="Submit" id="Submit" value="Search" />
</form>

search_result.php :

<?php

$host       ="localhost";
$username   ="XXXXXXXXXXX";
$password   ="XXXXXXXXXXX";
$db_name    ="XXXXXXXXXXXX";
$tbl_name   ="reg_add";

/* Connect to MySQL database */
mysql_connect("$host", "$username", "$password") or die("Error connecting to database");
mysql_select_db("$db_name")or die("Error selecting database");

$error = '';

if (isset($_POST['Submit'])) {


    if (!empty($_POST['reg'])) {

        $record = $_POST['reg']; // if coming from e.g. a form

        $query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
        $result = mysql_num_rows($query);

        if ($result != 0) {

            $row = mysql_fetch_array($query);

            $first_name = $row['first_name'];
            $last_name = $row['last_name'];
            $reg = $row['reg'];

        } else {

            $error = 'No result have been found!';

        }

    } else {

        $error = 'You have not entered the search field, <a href="javascript:history.back(1)">Go back</a>.';

    }
}

if (!empty($error)) { echo $error; } 
?>


<input  name="reg" value="<? echo $record; ?>">

<input  name="first_name" value="<? echo $first_name; ?>">

<input  name="last_name" value="<? echo $last_name; ?>">

Answer by Starx

Syntax of mysql_result() is wrong. According to the manual, it should be

string mysql_result ( resource $result , int $row [, mixed $field = 0 ] )

SO the correct way to use it would be like

mysql_result($result, 1, "first_name");
March 18, 2012

How do I add text inside of an input field using jQuery?

Question by John Anderson

This should be easy, but nothing seems to work. I have a jQuery function that contains two parameters. The first parameter (pageID) is supposed to be inserted into a hidden field. It works correctly. I want the second parameter to be inserted into an html input field. That is, I want to see the contents of the parameter ‘pageName’ inside the form field as if I had just typed it in myself. Here is the code:

function insertIntoHiddenField(pageID,pageName)
{
    $('input[name=page_id]').val(pageID);
    $('input[name=page_name]').val(pageName);
}

When I look at the source code, I find that the value attribute in the input tag contains the value from the parameter ‘pageName’. But, the input field itself is empty. I want to see that value in the input field. What am I doing wrong?

By the way, I also tried (without success) the following:

$('input[name=page_name]').text(pageName);
$('input[name=page_name]').innerhtml(pageName);
$('input[name=page_name]').append(pageName);

Answer by Starx

Give an id to the hidden element. Just to be on the safe side and use .val()

$('#page_name').val('newtext');
$('#page_id').val('newtext');

Demo

However, your method works perfectly for me. Check here

March 10, 2012

How do you stop people from editing a disabled inputbox?

Question by 001

When a input textbox is disabled, you can not enter any values into the Inputbox.

However if you use google chrome and right click on the inputbox then click on inspect, you can change the values.

How do you stop people from editing a disabled inputbox?

Answer by Starx

There is no way you can stop people from changing its state and sending the data. There are two way you can do this

  1. Do not display the disabled input at all. Just like what Zend_Form does.
  2. Check the field when the form was submitted and remove it.
...

Please fill the form - I will response as fast as I can!