Hi,

I am wondering if anyone tried to mix PHP and Javascript innerHTML elements to change certain elements to decrease the number of submits without losing data when passing through forms.

For some reason, I kept getting errors in my code and I could only get PHP to submit the form one submit after another. Is it possible just to change one portion of the form element here and interact with PHP using Javascript?

Here is the code,

<html>
<head>
<title>Data Enter</title>
 <script>
 function change(field){	
  
  previousInnerHTML = document.getElementById('start').innerHTML = "<p>Hello</p>";
<?php

$query="SELECT name FROM end_from WHERE type='" . $type2 . "'";
  $result = mysql_query ($query);  
 
 previousInnerHTML  = previousInnerHTML.concat("<select name='end_location'>";
  while($nt=mysql_fetch_array($result)){//Arrayor records stored in $nt
 previousInnerHTML += "<option value=$nt[0]>$nt[0]</option>";

}
  previousInnerHTML += "</select></p>";// Closing of list box
 
?>

}
</script>
</head>
<body>
<?php

  // Make a MySQL Connection
  mysql_connect("localhost", "xxx", "xxxx") or die(mysql_error());
  mysql_select_db("xxx") or die(mysql_error());

  $type = $_POST['type'];
  $start_time = $_POST['start_time'];
  
  if (isset($_POST['submit'])) { // if page is not submitted to itself echo the form
    echo $type . " " . $start_time . "\n";
  ?> 
  <form action="test2.php" method="post" name="start">
  <p><div id="start">
  <?php
  $query="SELECT name FROM start_from WHERE type='" . $type . "'";
  $result = mysql_query ($query);
  echo "<select name='start_location'>"; // printing the list box select command
  while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  echo "<option value=$nt[0]>$nt[0]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?> 
  </div>  
  <br/>
    <input type="hidden" name="start_time" value="<?=$start_time?>"/>
    <input type="submit" name="submit" value="Submit" >
  </p>
</form>
 
<?   } 
  
  else {
  ?>

  <form action="" name="form1" method="post">
  <p>
    What Time Would You Like to Start? <input type="text" name="start_time" size="25"/><br />
    Select the type for your starting point:<br>
   <div id="start">
  <input type="radio" value="Apartment" name="type" onchange="check(document.form1.type)"/>Apartment
  <input type="radio" value="Grocery" name="type" onchange="check(document.form1.type)"/> Grocery </div>    
   
    <input type="submit" name="submit" value="Submit" >
  </p>
</form>

<?
}
?>

Have I done something wrong here?
Thanks for your help.

Recommended Answers

All 3 Replies

The first part of the PHP code doesn't seem to do anything at the current position (i.e. it does not echo the previousInnerHTML variable), but I assume that it should echo the list of options in the JS function.

What exactly goes wrong? Can you see the initial list when opening the page?

Also, I see that the query at line 10 uses the variable $type2, which is not set in your script.

Oops, that should have been $type and not $type2.

To answer your question, it only gives me the initial set up of what I have, and does not add more information based on the way I want it.

Does this answer your question?
Thanks for your help.

am wondering if anyone tried to mix PHP and Javascript innerHTML elements to change certain elements to decrease the number of submits without losing data when passing through forms.

For some reason, I kept getting errors in my code and I could only get PHP to submit the form one submit after another. Is it possible just to change one portion of the form element here and interact with PHP using Javascript?

The bottom line is that once the form is displayed in the browser that is it.
As far as I know , the elements can't be erased or changed.
You can, however, create NEW elements and/or subform windows on the fly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.