Jump to content

[SOLVED] looping results from 1 form to update sql multiple times


farkewie

Recommended Posts

Hi i have posted about this before and with some help i have managed to get it almost working

 

it is almost working now, it is echoing data out..

 

only problem it always echo'es 4 "newlink" lines out even though there are only 3 form fields and the "for" statment is set to 3.

also if i only fill the form out once it still echo'es 4 times

 

 

 

here is my form

 


<form id="form1" name="form1" method="get" action="{$_SERVER['PHP_SELF']}"> 
  <table> 
    <tr> 
      <td><label> 
        <input type="text" name="text_1" id="text_1" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_1" id="link_1" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_1" id="location_1" /> 
      </label></td> 
    </tr> 
    <tr> 
      <td><label> 
        <input type="text" name="text_2" id="text_2" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_2" id="link_2" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_2" id="location_2" /> 
      </label></td> 
    </tr> 
    <tr> 
      <td><label> 
        <input type="text" name="text_3" id="text_3" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_3" id="link_3" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_3" id="location_3" /> 
      </label></td> 
    </tr> 
  </table> 
    <label> 
    <input type="submit" name="Submit" id="Submit" value="Submit" /> 
    </label> 
  </p> 
</form> 

 

 

here is my script

 

<?php

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

    $inputs = array();
    for ( $i = 0; $i <= 3; $i++ )
    {
// i have changed the code from ( isset($_GET['text_' . $i]) ) to the below to try and resolve 
        $inputs[$i]['text'] = ( !empty($_GET['text_' . $i]) ) ? $_GET['text_' . $i] : '';
        $inputs[$i]['link'] = ( !empty($_GET['link_' . $i]) ) ? $_GET['link_' . $i] : '';
        $inputs[$i]['location'] = ( !empty($_GET['location_' . $i]) ) ? $_GET['location_' .
            $i] : '';
    }





        foreach ( $inputs as $row )
        {
            echo 'New link ' . $row['text'] . '   --    ' . $row['link'] . '    --    ' . $row['location'] .
                '<br />';
        }
    }

?>

 

result from filling all 3 form fields out.

 

New link -- --

New link Link 1 -- http://link1.com -- Location 1

New link Link 2 -- http://link2.com -- Location 2

New link Link 3 -- http://link3.com -- Location 3

 

 

 

result from filling one of the three forms out.

New link -- --

New link Link 1 -- http://link1.com -- Location 1

New link -- --

New link -- --

Link to comment
Share on other sites

print_r($inputs);

 

during the for loop where it is created

 

and after your'e done creating it

 

it is echoing

echo 'New link ' . $row['text'] . '  --    ' . $row['link'] . '    --    ' . $row['location'] .

                '<br />';

 

4 times because there are 4 elements in the $inputs array, figure out where/when the extra element gets added by outputting in all places you use it, track it down

 

you can also print_r($_GET) to validate what you think is being inputted

Link to comment
Share on other sites

this statement

 

 

    for ( $i = 0; $i <= 3; $i++ )

 

has to be

 

    for ( $i = 1; $i <= 3; $i++ )

 

since your counter is starting from 1

 

hope its helpful

 

thank you very helpful!

 

only problem is it is still choing new link 3 times even if i only add one in the form.

 

ive tried adding $inputs[$i] = !empty($inputs);

 

but that just makes it echo 4 lines again

i also tried changing

the "foreach function to this

foreach ( !empty($inputs) as $row )

 

but just errors out.

Link to comment
Share on other sites

try this:

(btw: it will not add it only if the text portion of form is blank, you can change this to check for all 3 or whatever...)

 

<?php

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

    $inputs = array();
    for ( $i = 0; $i <= 3; $i++ )
    {
// i have changed the code from ( isset($_GET['text_' . $i]) ) to the below to try and resolve 
	if (!empty(trim($_GET['text_'.$i]))) {
		$t = $_GET['text_'.$i];
		$l = $_GET['link_'.$i];
		$loc = $_GET['location_'.$i];
		$inputs[$i] = array('text' => $t, 'link' => $l, 'location' => $loc);
	}
    }


        foreach ( $inputs as $row )
        {
            echo 'New link ' . $row['text'] . '   --    ' . $row['link'] . '    --    ' . $row['location'] .
                '<br />';
        }
    }

?>

Link to comment
Share on other sites

Hey thanks for that but now i get Fatal error

 

Fatal error: Can't use function return value in write context in C:\Users\Tyron\Desktop\public_html\form.php on line 55

 

line 55

 

<?php
    if (!empty(trim($_GET['text_'.$i]))) {
?>

i changed it to

if (!empty($_POST['text_'.$i])){

it works now

 

i added some checking to the script and changed it all to POST for cleaner URL,

 

Here is allmy code this has been a learning curve so any feedback att all on better ways i could have done things would be great.

 



<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo <<< EOT
<form id="form1" name="form1" method="POST" action="{$_SERVER['PHP_SELF']}"> 
  <table> 
    <tr> 
      <td><label> 
        <input type="text" name="text_1" id="text_1" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_1" id="link_1" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_1" id="location_1" /> 
      </label></td> 
    </tr> 
    <tr> 
      <td><label> 
        <input type="text" name="text_2" id="text_2" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_2" id="link_2" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_2" id="location_2" /> 
      </label></td> 
    </tr> 
    <tr> 
      <td><label> 
        <input type="text" name="text_3" id="text_3" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="link_3" id="link_3" /> 
      </label></td> 
      <td><label> 
        <input type="text" name="location_3" id="location_3" /> 
      </label></td> 
    </tr> 
  </table> 
    <label> 
    <input type="submit" name="Submit" id="Submit" value="Submit" /> 
    </label> 
  </p> 
</form> 
EOT;

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

    if ((!empty($_POST['text_1'])) || (!empty($_POST['link_1'])) || (!empty($_POST['location_1']))) {
        if ((empty($_POST['text_1'])) || (empty($_POST['link_1'])) || (empty($_POST['location_1']))) {
            echo "You must add link name, link address and link location for each link you add.";
            exit;
        }
    }
    if ((!empty($_POST['text_2'])) || (!empty($_POST['link_2'])) || (!empty($_POST['location_2']))) {
        if ((empty($_POST['text_2'])) || (empty($_POST['link_2'])) || (empty($_POST['location_2']))) {
            echo "You must add link name, link address and link location for each link you add.";
            exit;
        }
    }
    if ((!empty($_POST['text_3'])) || (!empty($_POST['link_3'])) || (!empty($_POST['location_3']))) {
        if ((empty($_POST['text_3'])) || (empty($_POST['link_3'])) || (empty($_POST['location_3']))) {
            echo "You must add link name, link address and link location for each link you add.";
            exit;
        }
    }

    $inputs = array();
    for ($i = 1; $i <= 3; $i++) {
        if (!empty($_POST['text_' . $i])) {
            $t = $_POST['text_' . $i];
            $l = $_POST['link_' . $i];
            $loc = $_POST['location_' . $i];
            $inputs[$i] = array('text' => $t, 'link' => $l, 'location' => $loc);
        }
    }


    foreach ($inputs as $row) {
        echo "<b>New Link: </b> <a href=\"" . $row['link'] . "\">" . $row['text'] .
            " </a>has been added.<br>It will be veiwable at location:  " . $row['location'] .
            "<br /><br />";
    }
}


?>


Link to comment
Share on other sites

Are you only submitting 3 each time? If you know the set amount each time and you are going to use the _1 at the end of each field I would do something like this

 

 


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

    for ($i = 1; $i <=3; $i++) {
        
        if ((!empty($_POST['text_$i'])) || (!empty($_POST['link_$i'])) || (!empty($_POST['location_$i']))) {
             if ((empty($_POST['text_$i'])) || (empty($_POST['link_$i'])) || (empty($_POST['location_$i']))) {
                 echo "You must add link name, link address and link location for each link you add.";
                 exit;
             }
        }
}

 

Though I'm not sure if I follow your if that you used, then again I'm new. It looks like you checking to see if they are not empty, and if they are not you then check again to see if they are empty. If the first if determines that the field is not empty there should be no reason to run another if to see if they are empty. I hope I might have helped in some way if not at least made your code shorter hehe

 

Cheers!

Stephen

Link to comment
Share on other sites

Try this...

 

The trick I used to check if the form has been filled correctly, which if i understand it correctly means that if they started to fill out one record for a link, all fields must not be empty, and then you prompt them for what they forgot to fill out

  Anyways, the way it works is that I add up the boolean values of empty() or !empty() which are 1 or 0. if they add up to 3 then all are empty. If they add up to 0 then all are not empty. What you want to look for is when they are more than 0 but less than 3, so this means that not all are empty, but its not completely filled out.

 

The flag variable is used to check if it ever entered the portion of code where it deems the form not properly filled out.

 

<?php
if (isset($_POST['Submit'])) {

#check that they correctly filled out fields
$flag = true;
for ($i = 1; $i<=3; $i++) {
	$a = empty(trim($_POST['text_'.$i)); //returns 1 on empty, 0 on not empty
	$b = empty(trim($_POST['link_'.$i));
	$c = empty(trim($_POST['location_'.$i));
	$d = $a + $b + $c;
	if ($d > 0 and $d < 3) {
		//user almost filled in a complete entry, so prompt which one they didn't fill out
		$flag = false;
		if ($a) {
			echo 'You did not add text for a link!<br>';
		}
		if ($b) {
			echo 'You did not add the address of the link!<br>';
		}
		if ($c) {
			echo 'You did not add the link location!<br>';
		}
	}
}

#if the flag is true, then they correctly filled out the form
if ($flag) {
	//fill array, a lot of this stuff here like checking for empty is depreciated since we have a nice method to check if form has been filled our correctly above ^^^   
	$inputs = array();
    for ($i = 1; $i <= 3; $i++) {
        if (!empty($_POST['text_' . $i])) {
            $t = $_POST['text_' . $i];
            $l = $_POST['link_' . $i];
            $loc = $_POST['location_' . $i];
            $inputs[$i] = array('text' => $t, 'link' => $l, 'location' => $loc);
        }
    }

	//output success message
    foreach ($inputs as $row) {
        echo "<b>New Link: </b> <a href=\"" . $row['link'] . "\">" . $row['text'] .
            " </a>has been added.<br>It will be veiwable at location:  " . $row['location'] .
            "<br /><br />";
    }
}
}
   
?>

Link to comment
Share on other sites

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.