Web Analytics Made Easy -
StatCounter Ajax - callback generated form doesnt work - CodingForum

Announcement

Collapse
No announcement yet.

Ajax - callback generated form doesnt work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ajax - callback generated form doesnt work

    hi,

    could someone help me with my problem please.
    I used javascript code from this site called ajaxpagefetcher.js for cloading content of div clicking on menu by a href.
    It work perfectly but I don't understand why callback generated form doesn't work like normal form. I mean if I call my php generator of form directly form work fine but if I call it by ajax way it doesn't .

    here is my generated code :

    Code:
    <form name="myForm" method="post" action="">
    <table  >
      <tr><td  >Name</td><td><input   type='text' name='Name'></td></tr><tr><td  >ICO</td><td><input   type='text' name='ICO'></td></tr><tr><td  >Type</td><td><select name='Type'><option value=1>PO</option><option value=2>FO</option></select></td></tr><tr>
    <td>
    <script language="JavaScript" type="text/JavaScript">
    function doSubmit ()
    	{
    	myForm.pressedSubmit.value = "y";
    	myForm.submit();
    	}
    </script>
    
    <input name="tablename" type="hidden" id="myTablename" value="subjects">
    <input name="numrows" type="hidden" id="tablename" value="8">
    <input name="pressedSubmit" type="hidden" id="pressedSubmit"> 
    </td>
    <td><input name="Submit" type="button" value="Submit" language="javascript" onclick="doSubmit()"></td></tr>
    </table>
    </form>

  • #2
    Change this line
    Code:
    <input name="Submit" type="button" value="Submit" language="javascript" onclick="doSubmit()">
    To:
    Code:
    <input name="Submit" type="submit" value="Submit" language="javascript">
    If your javascript does something (Can't see what from posted code), then add it back in.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      <input language="javascript">

      what is that attribute supposed to do?

      And myForm is only a variable like this in Internet Explorer (and only god knows why the put it in there). For all normal browsers you'll have to qualify using "document." prefix.

      If your DOCTYPE is XHTML you must not use a name attribute for form elements. Then you'll have to give the form an id attribute and use document.getElementById() instead.

      language="JavaScript" is also deprecated for the script tag.

      Comment


      • #4
        <input language="javascript">

        what is that attribute supposed to do?

        And myForm is only a variable like this in Internet Explorer (and only god knows why they put it in there). For all normal browsers you'll have to qualify using "document." prefix.

        If your DOCTYPE is XHTML you must not use a name attribute for form elements. Then you'll have to give the form an id attribute and use document.getElementById() instead.

        language="JavaScript" is also deprecated for the script tag.
        Last edited by devnull69; Aug 22, 2011, 04:18 PM.

        Comment


        • #5
          thanks both for reply and help but code still doesn't work by calling by ajax even directly it works..
          what I'm trying to is to automaticly generate editable form to insert record into table I choose like parameter for function...

          by this code I create parameters for function at the end of code to pass it forward :
          Code:
          <?php
          include 'gen_new_class.php';
          if (isset($_SESSION['gen_new']))
          {
          	$tablename = $_SESSION['gen_new'];
          	$tableStyle = ' ';
          	$editStyle = ' ';
          	$fieldNameStyle = ' ';
          
          include 'db.class.php';
          
          if (!mysql_connect($db_host, $db_user, $db_pwd))    
          {
          	die("Can't connect to database");
          }
          else
          {
          	$db = mysql_connect($db_host, $db_user, $db_pwd);
          }
          
          if (!mysql_select_db($database))  
          {
          	die("Can't select database");
          }
          else
          {
          	
          	mysql_select_db($database);
          	$dbName = $database;
          }
          
          
          $result = mysql_query("SHOW COLUMNS FROM $tablename WHERE `key`='MUL'");
          $num = mysql_num_rows($result);
          
          $i = 1;
          
          $namearr = array();
          $idarr = array();
          
          while($row = mysql_fetch_array($result))
          {
            $appendix = "" . $row['Field'] . "=";
            $x = $row['Field'];
            $rs0 = mysql_query("SELECT * FROM (SELECT REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE b WHERE COLUMN_NAME='" . $x . "') a");
           
           
            while($rw0 = mysql_fetch_array($rs0))
          	{
          		$table = $rw0['REFERENCED_TABLE_NAME'];
          	}
          	
          	
          	mysql_query( "SET NAMES cp1250");
           	$rs = mysql_query("SELECT ID, Name FROM (test.$table)");
           	$num2 = mysql_num_rows($rs);
          
           	$y = 1;
           	while($rw = mysql_fetch_array($rs))
          	{
          		$appendix .= $rw['Name'] ;
          				if ($y == $num2)
          			{
          				$appendix .= "";
          			}
          		  else
          			{
          				$appendix .= ",";
          			}
          			$y++;
          			
          			$idarr[$rw['Name']] = $rw['ID']; 
          	}
             
            $i++;
              
          
          		if (!isset($namearr[$row['Field']])) 
          		{  
          		$namearr[$row['Field']] = $appendix; 
          		} 
          		else 
          		{  
          		$namearr[$row['Field']] += $appendix; 
          		}
          
          }
          
          /*
          while (list($key, $value) = each($namearr)) {
             echo "Key: $key; Value: $value<br />\n";
          }
          
          while (list($key, $value) = each($idarr)) {
             echo "Key: $key; Value: $value<br />\n";
          }
          */
          
          
          $_SESSION['idcka']=$idarr;
          
          extract($namearr);
          
          genericSubmit( $tablename, $tableStyle, $editStyle, $fieldNameStyle, $Type, $Subtype, $City, $Country, $Dodavatel, $Subdodavatel, $Zakaznik );
          
          
          }
          else
          {
          	
          }
          
          
          ?>

          ... and here is code with called function :
          Code:
          ...
          function genericSubmit ($tablename, $tableStyle, $editStyle, $fieldNameStyle)
          	{
          	global $db, $dbName;
          	//mysql_query( "SET NAMES utf8");
          	$fields = mysql_list_fields($dbName, $tablename, $db); 	
          	$columns = mysql_num_fields($fields); 
          	
          
          	// Using variable args to collect fields that need to be filled with pre-existing values,
          	//  not entered by the user in edit boxes.
          	$numArgs = func_num_args(); // check for extra variable arguments
          	//echo $numArgs;
          	$argLoop = 4;  // known start of variable args
          	while ($argLoop < $numArgs)  // fill an array with the variable args
          		{
          		$arg = func_get_arg ($argLoop);
          	
          		// Remove any spaces
          		$arg = str_replace ( " ", " ", $arg);
          		
          		// Seperate into name - value pairs and place in an array 'args'		
          		list ($argName, $argValue) = explode ("=", $arg );
          		$args[$argName] = $argValue;  // this is a PHP key => value pair array
          		
          		++$argLoop;
          		}
          	?>
          <form name="myForm" method="post" action="">
          <table <?php echo ($tableStyle) ?>>
            <?php
          	$fieldLoop = 0;
          	$actualColumns = 0;
          	// Iterate through the fields in the table referred to in $tableName
          	
          	while ($fieldLoop < $columns)
          		{
          		$myFieldName = mysql_field_name ($fields, $fieldLoop);
          		$fieldType = mysql_field_type($fields, $fieldLoop);
          		
          		// Define these here, as we will be using in possible 3 times in the following IF statements
          		$autoIncrementState = strstr ( mysql_field_flags($fields, $fieldLoop), "auto_increment" );		
          		$argPresetExists = isset ($args[$myFieldName]);
          		//echo "presets:" . $argPresetExists;
          		// Set a prefix string for editbox names to identify data type
          		if ( $fieldType == "blob" || $fieldType == "string" || $fieldType == "date" || $fieldType == "intdate")
          			{
          			$typePrefix = "";
          			//$typePrefix = "s_";
          			}
          		else if ($fieldType == "int" || $fieldType == "string")
          			//$typePrefix = "i_";
          			$typePrefix = "";
          
          		// - if value of a field was given as FALSE in that list, DON'T display it
          		// - if a name/value pair was given for this field, write it as a hidden input element
          		// - if an autoincrement type, which is up to the database to maintain, DON'T display it
          		if ($autoIncrementState )  {} // don't display - it's an autoincrement field
          	 // No value preset in the variable args list OR there are MULTIPLE values preset for this field, DISPLAY field as editbox or if MULTIPLE then combo box or radio group as appropriate
          		else if (!$argPresetExists || @strstr ($args[$myFieldName], ",") )
          		 	{
          			++$actualColumns;
          ?><tr><td <?php echo $fieldNameStyle; ?>><?php echo $myFieldName; ?></td><td><?php
          			// Check for the data type of the field, and append the appropriate prefix to the name of the
          			//   input element name, so that the do-gen-submit.php page will know whether to wrap quotes
          			//   around the VALUE for this field in the INSERT statement
          
          			// Check for multiple choices for a field, indicated by a comma in the value
          			if ( @strstr ($args[$myFieldName], ",") )  // Does the current value contain a comma?
          				{
          					//echo $args[$myFieldName];
          				// If there is a comma, then there must be a list in the value, separate them out and write them to a radio group or combo box.
          				$multipleChoice = explode (",", $args [$myFieldName] );
          
          				// Now choose either a Radio group ot a Combo box, depending on the number of choices
          				if (count ($multipleChoice) < 1)
          					{
          					// place in a table
          					echo "<table><tr>";
          					foreach ($multipleChoice as $myChoice) // do a radio group
          						
          						echo "<td><input type='radio' name='" . $myFieldName . "' value='" . $myChoice . "'></td><td width=15></td>";
          						
          					echo "</tr></table>";
          					}
          				else	// do a Combo box
          					{
          					echo "<select name='" . $myFieldName . "'>";
          					foreach ($multipleChoice as $myChoice)
          					{
          											
          						echo "<option value=" . $_SESSION['idcka'][$myChoice] . ">" . $myChoice . "</option>";
          						//echo "<option>" . $myChoice . "</option>";
          					}
          					echo "</select>";
          					}					
          				}
          			else
          				echo ("<input " . $editStyle . " type='text' name='" . $typePrefix . $myFieldName . "'>");
          ?></td></tr><?php
            			}
          		// this is a field we already have values for, from the variable arg's list
          		else if ($argPresetExists && $args[$myFieldName] != "FALSE")
          			{
          			echo "<input type='hidden' name='";
          
          			// Check for the data type of the field, and append the appropriate prefix to the name of the
          			//   input element name, so that the do-gen-submit.php page will know whether to wrap quotes
          			//   around the VALUE for this field in the INSERT statement
          			echo ( $typePrefix );
          			echo $myFieldName . "' value='" . $args[$myFieldName] . "'>";
          			++$actualColumns;			
          			}
          			
          		++$fieldLoop;
          		}	
          ?>
          <tr><td>
          <script language="JavaScript" type="text/JavaScript">
          function doSubmit ()
          	{
          	myForm.pressedSubmit.value = "y";
          	myForm.submit();
          	}
          </script>
          <input name="tablename" type="hidden" id="myTablename" value="<?php echo ($tablename); ?>">
          <input name="numrows" type="hidden" id="tablename" value="<?php echo ($actualColumns); ?>">
          <input name="pressedSubmit" type="hidden" id="pressedSubmit"> </td>
          <td><input name="Submit" type="submit" value="Submit" language="javascript"  onclick="doSubmit()" ></td></tr>
          </table>
          </form><br>
          <?php
          //<td><input name="Submit" type="button" value="Submit" language="javascript" onclick="doSubmit()"></td></tr>
          }
          
          	// -------------- DO THE INSERT WHEN THE FORM IS SUBMITTED ------------
          if ( isset ($_POST ['pressedSubmit']) && $_POST ['pressedSubmit'] == "y" )
          	{
          	global $db, $dbName;
          	$fieldValues = "";
          	$fieldNames = "";	
          	$fieldLoop = 0;
          	foreach($_POST as $key => $value)
          		{
          		if ($fieldLoop < $_POST['numrows'])
          			{
          			if (substr ($key, 0, 2) == "s_")
          				$fieldValues .= "'" . $value . "' , ";
          			else
          				$fieldValues .= "'" . $value . "', ";
          		
          			++$fieldLoop;
          			}
          		}
          		
          	$fieldLoop = 0;
          	foreach($_POST as $key => $value)
          		{
          		if ( $fieldLoop < $_POST['numrows'] )
          			{
          			$fieldNames .= substr($key, 0, strlen($key) - 0) . ", ";
          			++$fieldLoop;
          			}
          		}
          
          	$fieldNames = substr ($fieldNames, 0 , -2 );
          	$fieldValues = substr ($fieldValues, 0 , -2 );	
          	$temp = "INSERT INTO " . $_POST['tablename'] . " (" .$fieldNames . ") VALUES (" . $fieldValues . ")";
          
          	echo "SQL:" . ($temp); // uncomment this line to see the INSERT statement generated by the function
          	// Do the INSERT
          	//echo "DB:" . $db;
          	mysql_query($temp, $db);  // Remember $db is your database connection variable
          	}
          ?>

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎