Jump to content

inset to same table from repeated form field


farkewie

Recommended Posts

Hello, i have seen this done once i just cant remember how i have been searching for ages im steppingin to new teritory so ive run out of things to try,

 

i want to be able to have 3 form fields "text" , "link" , "location"

 

but i want to have the same field 5 time so a user can add up to 5 links on the one form

 

i have made a lot of different attempts, here is my last.

 

i dont need someone to do it for me im happy to be just given a a link to the info i need,

 


<?php
echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
  <table>
    <tr>
      <td><label>
        <input type="text" name="text[]" id="text[]" />
      </label></td>
      <td><label>
        <input type="text" name="link[]" id="link[]" />
      </label></td>
      <td><label>
        <input type="text" name="location[]" id="location[]" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text[]" id="text[]" />
      </label></td>
      <td><label>
        <input type="text" name="link[]" id="link[]" />
      </label></td>
      <td><label>
        <input type="text" name="location[]" id="location[]" />
      </label></td>
    </tr>
    <tr>
      <td><label>
        <input type="text" name="text[]" id="text[]" />
      </label></td>
      <td><label>
        <input type="text" name="link[]" id="link[]" />
      </label></td>
      <td><label>
        <input type="text" name="location[]" id="location[]" />
      </label></td>
    </tr>
  </table>
  <label></label>
  <p> </p>
  <p>
    <label>
    <input type="submit" name="button" id="button" value="Submit" />
    </label>
  </p>
</form>
FRM;

$text = array($_GET['text']);
$link = array($_GET['link']);
$location = array($_GET['location']);

echo "<pre>";
foreach ($array_link as $text){
//SQL Statment will go here once i get the simple echo working..
echo "newlink {$text} {$link} {$location}";
}
?>

 

as always thank you for your help.

Link to comment
Share on other sites

That is the wrong way to do this.

 

You should not use [] on those inputs, if the user neglects to fill in an input you have no way to know which value with correspond to which input when processing.

 

Change your input name attributes to something like this name="text_1", then you can do a for loop through them like

if(isset($_GET['submit'])){
for($i = 0; $i >= 5; $i++){
   $inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
   $inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
   $inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
}
}
[code]

Then you can loop through like this
[code]
foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location']'<br />';
}

 

You should change from $_GET to $_POST for this many values I think

 

[/code][/code]

Link to comment
Share on other sites

Hi thanks for that.

 

its still not working, i have only put it at GET so i can see what is happening,

 

also is it possible to break it down so i can understand it for futre use?

 

 

here is code and errors.

 


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

echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
  <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>
FRM;

if(isset($_GET['Submit'])){
for($i = 0; $i >= 5; $i++){
   $inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
   $inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
   $inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
}




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

 

 

Notice:  Undefined variable: inputs in F:\Documents\public_html\testing\index.php on line 59

 

Warning:  Invalid argument supplied for foreach() in F:\Documents\public_html\testing\index.php on line 59

 

 

Thank you

Link to comment
Share on other sites

EDIT:

 

ADDING LATEST CODE..

 

 


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

echo <<<FRM
<form id="form1" name="form1" method="get" action="index.php">
  <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>
FRM;

if(isset($_GET['Submit'])){
for($i = 0; $i >= 3; $i++){
   $inputs[$i]['text'] = (isset($_GET['text_'.$i])) ? $_GET['text_'.$i] : '';
   $inputs[$i]['link'] = (isset($_GET['link_'.$i])) ? $_GET['link_'.$i] : '';
   $inputs[$i]['location'] = (isset($_GET['location_'.$i])) ? $_GET['location_'.$i] : '';
}




foreach($inputs as $row){
echo 'New link '.$row['text'].' '.$row['link'].' '.$row['location'].'<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.