#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Newbie in php,,,please help


    hello all,
    please take a look at my code and let me know wherei am going wrong..the following code doesnt work..
    my HTML.
    Code:
    <form method="post" action="Input1.php" name="form">
     
     
     <label> New Data set: </label>
     
        <input type="radio" name="url" value="NetOptInput2.html" id="ex1" required/> Yes
        <input type="radio" name="url" value="NetOptResult2.html" id="ex2" required/> No
        
    <br><br><br>
     <label>Dataset description:
    </label>
     <input type="text" name="Dataset" id="field1" size="30" placeholder="" readonly><br><br><br>
     <label>Token Number : </label><input type="text" name="Token Number" id="field2" size="6" placeholder="" readonly><br><br><br>
     
    <div style="text-align: center"><br>
      <input type="Submit" name="submit" value="Submit" class="submit">
     <div class="spacer"></div>
    My PHP
    PHP Code:
    <?php
    $savedata 
    $_REQUEST['savedata'];
    if (
    $savedata == 1){ 
    $data $_POST['Dataset'];
     
    $file "C:/wamp/www/NetOptUI2/input.txt"
     
    $fp fopen($file"a") or die("Couldn't open $file for writing!");
    fwrite($fp$data) or die("Couldn't write values to file!"); 
     
    fclose($fp); 
    echo 
    "Saved to $file successfully!";
     
    }
    ?>
    my javascript inside html
    Code:
    <script type="text/javascript">
      $(function(){
       
         $('form').submit(function(event){
       event.preventDefault();
       window.location = $('input[type=radio]:checked').val();
       });
       });
     </script>
     <script type="text/javascript">
     $(function(){
        $("#ex1, #ex2").change(function(){
            $("#field1, #field2").val("").attr("readonly",true);
            if($("#ex1").is(":checked")){
                $("#field1").removeAttr("readonly");
                $("#field1").focus();
            }
            else if($("#ex2").is(":checked")){
                $("#field2").removeAttr("readonly");
                $("#field2").focus();   
            }
        });
    });
    </script>
    Thanks in advance
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    Originally Posted by kjpr
    the following code doesnt work..
    I love precise error descriptions.

    What is the error? Are you getting an error message? Then what does it say? Or is it just unexpected behaviour? Then what did you expect?

    I could think of many different ways how this could go wrong. But since we don't have access to your PC to see what actually happens, I fear you have to tell us.
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    ITs not giving any errors. but, its not even creating any input.txt file.so, i am not sure whats happenning?could you please run it in your system by changing the path and let me know if you see any errors in the log?thanks for looking into the issue
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The PHP scripts works fine on my PC -- when I set savedata and Dataset manually.

    The form doesn't make a lot of sense, though. Where's your savedata field? Without that, the code won't even be executed. Why is the Dataset field empty and not writable? That means your script will tell you that it "couldn't write to the file" (which is kind of a lie).

    You should get familiar with the developer tools of your browser and basic debugging techniques (like using var_dump() to check the content of a variable). The very first step when "nothing happens" is to check the data your browser sends to the script.
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0
    Thanks for your help.By looking at my code, you must have understood that i am very new to the web development.I will clearly explain my task.
    In my HTML, when the user clicks Yes Radiobutton , Dataset form gets enabled for writing and also depending upon the yes button, i have written a function in javascript to navigate the user to another url after hitting the submit button.
    likewise, if theuser clicks no, the token number field gets enabled and after hitting submit, the user gets navigated to a different url.
    i also added the php script to get into action,once the user hits submit.
    so, do you think, i shouldnt have associated submit button to javascript and PHP?if not, i dont know how to get that functionality?
    Coming to the PHP sript,i tried "savedata" value before and i forgot to change it to submit..now, i changed it to submit.thanks for catching that. i tried to debug in the browser and found that the php script is not getting instantiated after hitting submit..instead it is going to the url that is directed by javascript function..
    please help me debug this,as this is very important to me on my work front. here ismy new php
    Code:
    <?php
    $submit = $_REQUEST['submit'];
    if ($submit == 1){ 
    $data = $_POST['Dataset'];
    
    $file = "C:/wamp/www/NetOptUI2/input.txt"; 
    
    $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!"); 
    
    fclose($fp); 
    echo "Saved to $file successfully!";
    
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo