Jump to content

Recommended Posts

I am using some code I have done before now its been a long day, and I am totally puzzled as to why the form will not send the data to mysql and then move onto the next page, via the header location.

 

At the moment, when I fill the form in and click submit it just stays on the same page and does not process anything. I am not sure why and where the issue is? Any help appreciated to get this moving forward.

 

Here is the code that processes the form to mysql etc..

<?
//# Include the connections script to make a database connection.
include("inc/connect.inc");

//# The form should post to itself.
if ( $_POST['submit'] ) {

$valid = 1;

$venue_name = $_POST['venue_name'];
if ( empty($venue_name) ) {
$valid = 0;
$venue_name_error = '<div class="formerror">Please Enter your Fishery Venue Name.</div>';
}

$address_1 = $_POST['address_1'];
if ( empty($address_1) ) {
$valid = 0;
$address_1_error = 'Please enter the first line of your address';
}

$address_2 = $_POST['address_2'];
$address_3 = $_POST['address_3'];

$town = $_POST['town'];
if ( empty($town) ) {
$valid = 0;
$town_error = 'Please enter your town/city';
}

$county = $_POST['county'];
if ( empty($county) ) {
$valid = 0;
$county_error = 'Please select your county';
}

$postcode = $_POST['postcode'];
if ( empty($postcode) ) {
$valid = 0;
$postcode_error = 'Please Enter your Postcode';
}

$email = $_POST['email'];

$telephone_1 = $_POST['telephone_1'];
if ( empty($telephone1) ) {
$valid = 0;
$telephone1_error = 'Please Enter your Main Contact Number';
}

$telephone_2 = $_POST['telephone_2'];
$fax = $_POST['fax'];

// End of error checking, all fields covered.

if ( $valid == 1 ) {

        # setup SQL statement
        $SQL = " INSERT INTO dbtable ";
        $SQL = $SQL . " (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES ";
        $SQL = $SQL . " ('$venue_name', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') ";

        #execute SQL statement
        $result = mysql_db_query( *****,"$SQL",$connection );

        # check for error
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

{
header("Location: http://www.domain.com/admin/admin.html");
exit;
}

}
}
?>

 

The form is pretty standard, here is the action form action part of the form:

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/198120-script-not-processing/
Share on other sites

Is your form validation in the same page as the form, also.. is your submit button got ' name="submit" ' within it?

 

Yes, the form validation and processing is before the html of the page, and the submit button has the name="submit" which I double checked as thought that might be the case, its just not processing, its error checking and staying on that part? Here is one of the form input fields:

<label for="address_2">Address 2</label> <input class="long" type="text" value="<?php echo $address_2; ?>" name="address_2" id="address_2" />

 

As the form is all filled in, its not giving any errors, the errors just to make sure people fill in what is required (further error checking to be done once I get this working ie. email).

 

what is happening now. You fill in the form and hit the submit button and the same page just reloads with the fields in the form filled in with what you typed first. So its stuck for some reason.

 

It is supposed to send the information to a sql query to process into a database, then redirect you to the next page. I am puzzled why it wont?

I might be able to help if I understood your query. Seems that your connection isn't automatically made, it's made when you do a query.

Do queries similar to that work, well... if it didn't, it'll bring up an mysql_error.

 

Confirm that queries similar to that DO work. Might have a few ideas.

 

Edit:

Does this echo execute when you run the script (and enter correct information)

 echo 'Correct'; 

Put the echo below the  if ( $valid == 1 ) { line.

Try this:  I made a few changes, don't know if they will affect the script behavior or not.

<?php
//# Include the connections script to make a database connection.
include("inc/connect.inc");

//# The form should post to itself.
if ( $_POST['submit'] ) {

$valid = 1;

$venue_name = $_POST['venue_name'];
if ( empty($venue_name) ) {
$valid = 0;
$venue_name_error = '<div class="formerror">Please Enter your Fishery Venue Name.</div>';
}

$address_1 = $_POST['address_1'];
if ( empty($address_1) ) {
$valid = 0;
$address_1_error = 'Please enter the first line of your address';
}

$address_2 = $_POST['address_2'];
$address_3 = $_POST['address_3'];

$town = $_POST['town'];
if ( empty($town) ) {
$valid = 0;
$town_error = 'Please enter your town/city';
}

$county = $_POST['county'];
if ( empty($county) ) {
$valid = 0;
$county_error = 'Please select your county';
}

$postcode = $_POST['postcode'];
if ( empty($postcode) ) {
$valid = 0;
$postcode_error = 'Please Enter your Postcode';
}

$email = $_POST['email'];

$telephone_1 = $_POST['telephone_1'];
if ( empty($telephone1) ) {
$valid = 0;
$telephone1_error = 'Please Enter your Main Contact Number';
}

$telephone_2 = $_POST['telephone_2'];
$fax = $_POST['fax'];

// End of error checking, all fields covered.

if ( $valid == 1 ) {

        # setup SQL statement
        $SQL = " INSERT INTO dbtable ";
        $SQL .= " (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES ";
        $SQL .= " ('$venue_name', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') ";

        #execute SQL statement
        $result = mysql_query( $SQL,$connection );

        # check for error
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }
	else {
	   header("Location: http://www.domain.com/admin/admin.html");
	   exit;
	}

}
}
?>

If your form is being redisplayed, then the code where the header() statement is at is not being executed.

 

What does your code on the rest of the page do when $valid is not equal to 1? In fact, to get the quickest solution to what the code on your page is doing or is not doing correctly, just post the whole actual code for that page.

 

Have you actually checked that your form causes $_POST['submit'] to be set?

 

Also, don't use .inc file extensions for included code. If someone guesses the name they can just browse to the file and see your database connection information. Always use a .php file extension for included code.

Thanks people. I will change that inc to php, I forgot that bit and have been aware of that.. thanks again.

 

Right now the problem seems to be with the valid == 1

 

I changed this to zero and I know that cancels out the error check, but it submitted the data, so my problem is around that area.

One or more of your validation tests is failing.

 

The reason I asked what the remainder of the code on the page is doing when $valid is not a 1 is because your code is apparently not displaying what validation errors did occur, which would help you pin down where the problem in the form or in the validation code is at.

I'm not sure if this works, but I changed the errors.

To echo an error in this code, you'll do

echo $error['errorname'];

 

here is the code

<?php
// Include the connections script to make a database connection.
include("inc/connect.inc");

// The form should post to itself.
if ( $_POST['submit'] ) {
   $venue_name = $_POST['venue_name'];
   $address_1 = $_POST['address_1'];
   $address_2 = $_POST['address_2'];
   $address_3 = $_POST['address_3'];
   $town = $_POST['town'];
   $county = $_POST['county'];
   $postcode = $_POST['postcode'];
   $email = $_POST['email'];
   $telephone_1 = $_POST['telephone_1'];
   $telephone_2 = $_POST['telephone_2'];
   $fax = $_POST['fax'];
   
   if ( empty($venue_name) ) {
      $error['venue_name_error'] = '<div class="formerror">Please Enter your Fishery Venue Name.</div>';
   }
   if ( empty($address_1) ) {
      $error['address_1_error'] = 'Please enter the first line of your address';
   }
   if ( empty($town) ) {
      $error['town_error'] = 'Please enter your town/city';
   }
   if ( empty($county) ) {
      $error['county_error'] = 'Please select your county';
   }
   if ( empty($postcode) ) {
      $error['postcode_error'] = 'Please Enter your Postcode';
   }
   if ( empty($telephone1) ) {
      $error['telephone1_error'] = 'Please Enter your Main Contact Number';
   }

   // End of error checking, all fields covered.

   if (!$error) {
       echo 'Correct';
        # setup SQL statement
        $SQL = " INSERT INTO dbtable ";
        $SQL = $SQL . " (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES ";
        $SQL = $SQL . " ('$venue_name', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') ";

        #execute SQL statement
        $result = mysql_db_query( *****,"$SQL",$connection );

        # check for error
        if (!$result) { 
         echo("ERROR: " . mysql_error() . "\n$SQL\n");    
      } else {
         header("Location: [url=http://www.domain.com/admin/admin.html]http://www.domain.com/admin/admin.html[/url]");
         exit;
      }
   }
}
?>

 

Again, you'll need the change the include to .php or whatever you are using.

This is a method I use, so if that wouldn't work, then the problem is else where, not related to the PHP.

Thanks for the change in script, have to say much easier than the way I have done it and simpler. Right, after doing that still no joy, so its got to be the form. Here is the form code relating to my original code though:

 

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<fieldset>
<legend>Main Details</legend>
<p>Please complete the form below</p>
<p><label for="venue_name">Venue Name</label> <input class="long" type="text" value="<?php echo $venue_name; ?>" name="venue_name" id="venue_name" maxlength="128" /><br><?php echo $venue_name_error; ?></p>
<p><label for="address_1">Address 1</label> <input class="long" type="text" value="<?php echo $address_1; ?>" name="address_1" id="address_1" /><br><?php echo $address_1_error; ?></p>
<p><label for="address_2">Address 2</label> <input class="long" type="text" value="<?php echo $address_2; ?>" name="address_2" id="address_2" /></p>
<p><label for="address_3">Address 3</label> <input class="long" type="text" value="<?php echo $address_3; ?>" name="address_3" id="address_3" /></p>
<p><label for="town">Town/City</label> <input class="long" type="text" value="<?php echo $town; ?>" name="town" id="town" /><br><?php echo $town_error; ?></p>

<p><label for="county">County</label> 
<select name="county" class="long">
	<option value="">Please Choose County</option>
<optgroup label="England">
	<option>Avon</option>
	<option>Bedfordshire</option>
	<option>Berkshire</option>
	<option>Buckinghamshire</option>
	<option>Cambridgeshire</option>
	<option>Cheshire</option>
	<option>Cleveland</option>
	<option>Cornwall</option>
	<option>Cumbria</option>
	<option>Derbyshire</option>
	<option>Devon</option>
	<option>Dorset</option>
	<option>Durham</option>
	<option>East Sussex</option>
	<option>Essex</option>
	<option>Gloucestershire</option>
	<option>Hampshire</option>
	<option>Herefordshire</option>
	<option>Hertfordshire</option>
	<option>Isle of Wight</option>
	<option>Kent</option>
	<option>Lancashire</option>
	<option>Leicestershire</option>
	<option>Lincolnshire</option>
	<option>London</option>
	<option>Merseyside</option>
	<option>Middlesex</option>
	<option>Norfolk</option>
	<option>Northamptonshire</option>
	<option>Northumberland</option>
	<option>North Humberside</option>
	<option>North Yorkshire</option>
	<option>Nottinghamshire</option>
	<option>Oxfordshire</option>
	<option>Rutland</option>
	<option>Shropshire</option>
	<option>Somerset</option>
	<option>South Humberside</option>
	<option>South Yorkshire</option>
	<option>Staffordshire</option>
	<option>Suffolk</option>
	<option>Surrey</option>
	<option>Tyne and Wear</option>
	<option>Warwickshire</option>
	<option>West Midlands</option>
	<option>West Sussex</option>
	<option>West Yorkshire</option>
	<option>Wiltshire</option>
	<option>Worcestershire</option>
</optgroup>
<optgroup label="Wales">
	<option>Clwyd</option>
	<option>Dyfed</option>
	<option>Gwent</option>
	<option>Gwynedd</option>
	<option>Mid Glamorgan</option>
	<option>Powys</option>
	<option>South Glamorgan</option>
	<option>West Glamorgan</option>
</optgroup>
<optgroup label="Scotland">
	<option>Aberdeenshire</option>
	<option>Angus</option>
	<option>Argyll</option>
	<option>Ayrshire</option>
	<option>Banffshire</option>
	<option>Berwickshire</option>
	<option>Bute</option>
	<option>Caithness</option>
	<option>Clackmannanshire</option>
	<option>Dumfriesshire</option>
	<option>Dunbartonshire</option>
	<option>East Lothian</option>
	<option>Fife</option>
	<option>Inverness-shire</option>
	<option>Kincardineshire</option>
	<option>Kinross-shire</option>
	<option>Kirkcudbrightshire</option>
	<option>Lanarkshire</option>
	<option>Midlothian</option>
	<option>Moray</option>
	<option>Nairnshire</option>
	<option>Orkney</option>
	<option>Peeblesshire</option>
	<option>Perthshire</option>
	<option>Renfrewshire</option>
	<option>Ross-shire</option>
	<option>Roxburghshire</option>
	<option>Selkirkshire</option>
	<option>Shetland</option>
	<option>Stirlingshire</option>
	<option>Sutherland</option>
	<option>West Lothian</option>
	<option>Wigtownshire</option>
</optgroup>
</select>
<br><?php echo $county_error; ?></p>

<p><label for="postcode">Postcode</label> <input class="short" type="text" value="<?php echo $postcode; ?>" name="postcode" id="postcode" /><br><?php echo $postcode_error; ?></p>
<p><label for="email">Email</label> <input class="long" type="text" value="<?php echo $email; ?>" name="email" id="email" /></p>
<p><label for="telephone_1">Main Telephone</label> <input class="long" type="text" value="<?php echo $telephone_1; ?>" name="telephone_1" id="telephone_1" /><br><?php echo $telephone_1_error; ?></p>
<p><label for="telephone_2">Other Telephone</label> <input class="long" type="text" value="<?php echo $telephone_2; ?>" name="telephone_2" id="telephone_2" /></p>
<p><label for="fax">Fax</label> <input class="long" type="text" value="<?php echo $fax; ?>" name="fax" id="fax" /></p>
<p class="submit"><input type="submit" name="submit" value="next step" /></p>
</fieldset>
</form>

The following line of code is missing an underscore in the variable name -

   if ( empty($telephone1) ) {

 

The reason the resulting 'Please Enter your Main Contact Number' message is not being displayed in the form to help you pin down this problem is because your variables names for the errors don't match between what you are setting and what you are displaying.

 

You are apparently trying to develop and debug code on a system that does not have error_reporting set to E_ALL and display_errors set to ON to get php to help you.

appreciate your time. I noticed that one and changed it, since posting the original code, I noticed a few issues that I corrected. I am getting no errors now, but still not submitting the data? as I found out its within the form somewhere.

 

I have produced the same script for another site, albeit different data and this works fine, the only difference being the previous one was done in tables with standard input fields and not using the css way by using fieldsets etc..

The form you posted in this thread submits correctly and once the error in the variable name is corrected, produces the expected INSERT query and attempts to execute it -

 

INSERT INTO dbtable (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES ('venue', 'address 1', 'address 2', 'address 3', 'town', 'Avon', 'post code', 'email', 'phone', 'other phone', 'fax')

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.