Jump to content

Multiple Array Problems passing


carbayon

Recommended Posts

I am looking for help on regards of a problem I am having passing arrays from the HTML form page to the PHP page.

 

This is what the link send;

 

marks.php?club=16&coordinates=decimal&markname[]=test&shortname[]=TST&description[]=testing&longitude[]12=&latitude[]=34&addmark=addmark&amount=1&send=send

 

This is a dynamic script, so I could be sending multiple sets of arrays; the amount variable counts how many "sets" are being sent to the PHP.

 

If it would be easier, I can also change the arrays to individuals to contiguous variables; i.e. markname1=... markname2=... markname3=.... This method has the problems that I would need to read "markname.counter" and I am also failing to get this correctly.

 

I know in the PHP code I have to use foreach, but do I need to declare the variable first? The form is being submitted using the ($_SERVER['PHP_SELF'])

 

If you need more information to solve the problem, please let me know.

 

Thanks in advance;

Ramón D Díaz

[carbayón]

 

 

Link to comment
Share on other sites

Thanks for the answer;

 

I am using GET on the test phace; when I pass arrays using the POST (or the GET) I get a blank page, the system is unable to handle the arrays.

 

With GET I can see the URL and thus I can see I am passing the variables correctly, thus my problem is on the PHP code I have.

 

This the PHP code, what I may missing? Why do I get a blank page?

?php

  //Start the session
  session_start();

  if (empty($_SESSION['logged'])){
    header('location:../index.php');
  }

  //Set Username value
  $username = $_SESSION['username'];
  
  $path = ($_SERVER['DOCUMENT_ROOT']);
  chdir($path);
  include '../lib/common.php';
  include '../lib/database.php';
  include '../lib/functions.php';

  include 'form_club.php';

  if(isset($POST['addclub'])){
    $msg_mark = "";

    //This is where my validation code goes

    if(empty($msg_mark)){

      //This is where code will go...
    
    }//if(empty($msg_mark))

  }//if(isset($_POST['addclub']))
  
?>

 

I know I have to initialise the arrays and I know I have to use foreach(); but I am not able to make this work correctly.

 

How

Link to comment
Share on other sites

OK, this is the code being sent from the HTML form;



<form name="addmark" method="post" action="/members/marks.php">

  <table width="100%">

    <tr>
     <td colspan="3" align="center"><i>[*] Mandatory Fields</i></td>
    </tr>

    <tr>
      <td width="50"> </td>
      <td>Sailing Club:<sup>*</sup></td>
      <td>
        <select name="club" style="width: 147px;">
          <option value="16" selected="selected">visual sailing</option>
        </select>
      </td>
    </tr>

    <tr>
      <td> </td>
      <td colspan="2" align="left">
        <input name="coordinates" value="decimal" onclick="getCoordinates('findCoordinates.php?coordinates='+this.value)" type="radio">  Decimal Degrees [53.3434 / -6.2732]
        <br>
        <input name="coordinates" value="time" onclick="getCoordinates('findCoordinates.php?coordinates='+this.value)" type="radio">  Degrees Minutes Seconds [N 53 20 36 / W 6 16 23]
        <br>
        <input name="coordinates" value="GPS" onclick="getCoordinates('findCoordinates.php?coordinates='+this.value)" type="radio">  GPS Reading [N 53 20.604 / W 6 16.392]
      </td>
    </tr>

    <tr>
      <td colspan="3" align="left">
        <div id="" style="display: block;">
            <input value="Remove Mark" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" type="button"><br>
            Mark Name:<sup>*</sup> <input name="markname[]" type="text">   
            Short Name:<sup>*</sup><input name="shortname[]" size="3" type="text"><br><br>
            Description:   <input name="description[]" type="text"><br><br>
            Longitude:<sup>*</sup>   <input name="longitude[]" size="8" type="text">
                
            Latitude:<sup>*</sup>   <input name="latitude[]" size="8" type="text"><br> 
            <hr width="75%" align="center">

        </div>
        <div id="writeroot"></div>
      </td>
    </tr>

    <tr>
      <td colspan="3" align="center">
        <input name="addmark" value="addmark" type="hidden">
        <input name="amount" value="2" type="hidden">
        <input value="Add another Mark" onclick="moreFields()" type="button">
        <input name="send" value="send" type="submit">
                    
        <input value="reset" type="reset">
      </td>
    </tr>

  <table>

</form>

 

The form is part of javascript that repeats itself when the Add Another Mark button is pressed, this way the system allows the user to add and undetermined amount of Marks at once in the system.

 

The repetition code was taken from http://www.quirksmode.org/dom/domform.html

 

I know I have to declare the array on the PHP page, but I am not sure how to do it, as the page is SELF loaded and the array will be reset.

Link to comment
Share on other sites

Your problem is that you are missing the underscore right after the dollar sign:

 if(isset($POST['addclub'])){

 

Should be

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

 

 

To ensure all the variables are passed in the POST/GET variables, use print_r() to see them

echo "<pre>";
print_r($_POST);
echo "</pre>";

Link to comment
Share on other sites

thanks for help;

 

I notice the _POST and POST error and I have corrected that; I have added the code you recommended and as long as I don't pass an array, I can see the _POST information;

Array
(
    [club] => 16
    [coordinates] => decimal
    [markname] => 
    [shortname] => 
    [description] => 
    [longitude] => 
    [latitude] => 
    [addmark] => addmark
    [amount] => 3
    [send] => send
)

 

But as soon as I change markname to markname[] the next time I submit the form I get a blank page.

 

Link to comment
Share on other sites

Another option that I have (using the original Javascript) is to increase the variable's name to make then unique;

Array
(
    [club] => 16
    [coordinates] => decimal
    [markname1] => 
    [shortname1] => 
    [description1] => 
    [longitude1] => 
    [latitude1] => 
    [markname2] => 
    [shortname2] => 
    [description2] => 
    [longitude2] => 
    [latitude2] => 
    [addmark] => addmark
    [amount] => 2
    [send] => send
)

 

This presents me with another problem; how can I read this variables on PHP? I can create a for loop since amount gives me the counter limit.

 

for (i=0; i < amount ; i +=1), but how can I rename the variable from markname to markname1, markname2....??

 

Regards

Link to comment
Share on other sites

firstly, try adding value="" to your text inputs

<div id="" style="display: block;">
<input value="Remove Mark" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" type="button"><br>
Mark Name:<sup>*</sup> 
<input name="markname[]" type="text" value="">   
Short Name:<sup>*</sup>
<input name="shortname[]" size="3" type="text" value=""><br><br>
Description:   
<input name="description[]" type="text" value=""><br><br>
Longitude:<sup>*</sup>   
<input name="longitude[]" size="8" type="text" value="">
    
Latitude:<sup>*</sup>   
<input name="latitude[]" size="8" type="text" value=""><br>
<hr width="75%" align="center">

</div> 

 

Second - These don't look like arrays to me. You are just collecting one bit of data. Now if you had checkboxes - yes, store that in array. If you for some odd reason want mutliple text data for every entry then you can use name="somevalue[]" but you need it on more than one input for it to be an array

Link to comment
Share on other sites

Adding the value didn't work correctly....

 

The variables become an array because I am filling the form multiple times; thus the variables become repeated and thus the value is overwritten.

 

When the page loads, the "master" form is loaded on the background;

- Mark Name --> $markname

- Shortname --> $shortname

- Description --> $description

- Longitude  --> $longitude

- Latitude    --> $latitude

 

When the user click on Add Another Mark a javascript is run and two things happen;

[*]the marks are renamed

[*]the form is presented on the screen

 

The first form has the variables;

- Mark Name --> $markname1

- Shortname --> $shortname1

- Description --> $description1

- Longitude  --> $longitude1

- Latitude    --> $latitude1

 

If the user click on Add Another Mark, then the javascript runs again an increments the number again;

 

The second form uses (unique) variables

- Mark Name --> $markname2

- Shortname --> $shortname2

- Description --> $description2

- Longitude  --> $longitude2

- Latitude    --> $latitude2

 

and the process repeats itself over and over.

 

The two choices I have are to read from $_POST all the individual variables using a for loop; or to change the JavaScript to rename the variables with [] rather than different numbers, this way if 10 forms are submitted, I would end up with markname[] = and the 10 values inside, rather than having markname1, markname2...markname10.

 

At this point and given the array seems to be complex to solve, I take the for loop solution, but this too has a problem; how can I use the for look to read $markneme#; how can I concatenate the name and create a variable.



for (i=0; i < amount; i+=){
  $temp = $.markname.counter;
  $markname = ($_POST($temp));
 
  // Do my SQL code
}

The above code would also work as I can re-iterate as many times as forms are sent, on each look I can read all the variables, process the SQL and then on the next loop over the i value increases and I can repeat the process.

 

I am looking to either (or any other) solutions that would allow me to read the forms sent in.

Link to comment
Share on other sites

each entry has it's own unique values that are not arrays.

 

entry 1 =

Mark Name = entry 1's mark name

Shortname = entry 1's short name

...

etc

 

entry 2 =

Mark Name = entry 2's mark name

Shortname = entry 2's short name

...

etc

 

and so on.

that means that you should have an array of entries and each entry will have an array of data - but the Mark name will NOT be an array. it is still unique per entry.

 

Also, you should not do the for loop the way that you show. We can use a foreach loop to loop through each entry if we need.

You mentioned a SQL code. What do you need from the DB??

 

Link to comment
Share on other sites

Sorry to be nu-sense on this, but how can I use the foreach loop[/] on this case?

 

The plan is to store the marks in the database with an UPDATE statement and write to the screen using the $msg_mark

 

The page is mostly done and completed, except for this "stupidity"; I could allow users to input one mark at a time and it will be OK, but since I started I want to complete the form that allows multiple input of marks at once.

 

The problem is still "there", how can foreach loop read the variables?

 

As per my prior post, there is only one array with all the data;

Array
(
    [club] => 16
    [coordinates] => decimal
    [markname1] => 
    [shortname1] => 
    [description1] => 
    [longitude1] => 
    [latitude1] => 
    [markname2] => 
    [shortname2] => 
    [description2] => 
    [longitude2] => 
    [latitude2] => 
    [addmark] => addmark
    [amount] => 2
    [send] => send
)

 

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.